Cannot resize linear layout children in android react native module

前端 未结 4 912
清酒与你
清酒与你 2021-02-13 21:31

Full code here

Video of correct behavior in java and incorrect in react native here

I have modified a linear layout to respond to touch by resizing the left chil

4条回答
  •  你的背包
    2021-02-13 21:59

    I had the same problem and I found the answer on this old reported issue on react native's repo.

    Add the following code in your custom layout and after that there is no need to even call requestLayout() or invalidate(). The changes are propagated as soon as you update your layout's LayoutParams. This solution is the same as the ones used in ReactPicker.java and ReactToolbar.java.

    @Override
    public void requestLayout() {
        super.requestLayout();
    
        // The spinner relies on a measure + layout pass happening after it calls requestLayout().
        // Without this, the widget never actually changes the selection and doesn't call the
        // appropriate listeners. Since we override onLayout in our ViewGroups, a layout pass never
        // happens after a call to requestLayout, so we simulate one here.
        post(measureAndLayout);
    }
    
    private final Runnable measureAndLayout = new Runnable() {
        @Override
        public void run() {
            measure(
                    MeasureSpec.makeMeasureSpec(getWidth(), MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(getHeight(), MeasureSpec.EXACTLY));
            layout(getLeft(), getTop(), getRight(), getBottom());
        }
    };
    

提交回复
热议问题