React Native: UI Resizing

I created a very simple native component of the Android user interface, and I want to update the size of its child view when the button from my responsive project is clicked. To be more precise, when this button is pressed, I send a command to mine SimpleViewManager, which, in turn, calls resizeLayout()my user view.

I can check if it is being called correctly resizeLayout(), but the layout does not change until I rotate the phone . Obviously, changing the orientation of the device triggers draw()my user view, but it also does invalidate(), which I indirectly call.

Other layout changes, such as changing the background color rather than resizing, work great.

My custom component is as follows:

public class CustomComponent extends RelativeLayout {
    public CustomComponent(Context context) {
        super(context);
        init();
    }

    public CustomComponent(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomComponent(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        inflate(getContext(), R.layout.simple_layout, this);
    }

    public void resizeLayout(){
        LinearLayout childLayout = (LinearLayout) findViewById(R.id.child_layout);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) childLayout.getLayoutParams();
        layoutParams.height = 50;
        layoutParams.width= 50;
        childLayout.setLayoutParams(layoutParams);

        invalidate();
    }
}

and simple_layout.xml looks like this:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/child_layout"
        android:layout_width="100dp"
        android:layout_height="50dp"
        android:background="#ffee11"/>
</RelativeLayout>

Any help would be greatly appreciated.

+4
source share
2 answers

After several days of searching, I finally found the answer to this long-standing question about responding to my own repo.

This solution is the same as in ReactPicker.java and ReactToolbar.java . I had to put the following code in my CustomComponent, and after that there is no need to even call requestLayout()or invalidate(). Changes are propagated as soon as I update my LayoutParams layout.

@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());
    }
};
+8

requestLayout();

.

.

forceLayout(), requestLayout() invalidate()

xml

<merge>

, CustomComponent RelativeLayout , , .

0

Source: https://habr.com/ru/post/1656602/


All Articles