FXCesinha FXCesinha - 3 months ago 48
Android Question

react-native wrapper for CoordinatorLayout and BottomSheetBehavior

I've been writing a native android module that wraps a BottomSheetBehavior.

A very simple BottomSheetBehavior can be implemented like this
https://gist.github.com/cesardeazevedo/a4dc4ed12df33fe1877fc6cea42475ae

The first thing that i faced, the entire page must be a child of a CoordinatorLayout and the BottomSheetBehavior at the end of it.

So i had to write 2 native modules,

<CoordinatorLayout />
and
<BottomSheetBehavior />
.

This is the bottomSheetBehavior wrapper.

BottomSheetBehaviorManager.java

public class BottomSheetBehaviorManager extends ViewGroupManager<BottomSheetBehaviorView> {

@Override
public BottomSheetBehaviorView createViewInstance(ThemedReactContext context) {
return new BottomSheetBehaviorView(context);
}
}


BottomSheetBehaviorView.java

public class BottomSheetBehaviorView extends RelativeLayout {

public BottomSheetBehaviorView(Context context) {
super(context);

int width = ViewGroup.LayoutParams.WRAP_CONTENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
// int height = 1000; // fixed a height works, it only slide up half of the screen

CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(width, height);
params.setBehavior(new BottomSheetBehavior());
this.setLayoutParams(params);

BottomSheetBehavior<BottomSheetBehaviorView> bottomSheetBehavior = BottomSheetBehavior.from(this);
bottomSheetBehavior.setHideable(false);
bottomSheetBehavior.setPeekHeight(200);
}
}


And my react component become like this.

index.android.js

return () {
<CoordinatorLayout style={{flex: 1}}>
<View><!--app--></View>
<BottomSheetBehavior>
<View style={{height: 300}}> <!--height doesnt work-->
<Text>BottomSheetBehavior !</Text>
</View>
</BottomSheetBehavior>
</CoordinatorLayout>
)


And it works!

react-native-bottomsheet-behavior

But i've been struggling to make the BottomSheet wrap their childs with
wrap_content
, it's was not supposed to slide the entire screen, it should only slide through the wrapped content (in this case a lorem ipsum text), it works with android components but it not working with react components. So, how to make a
RelativeLayout
to wraps a react
<View style={{height: 300}} />
component? I also have tried to implemented some measure shadownode, but didn't worked as expected, i don't know how they work.

I've added this example on my github for everyone wants to try it.
https://github.com/cesardeazevedo/react-native-bottom-sheet-behavior

Answer

After a lot of debugging, finally i got it, i had to do 2 things, first was to override the onMeasure function and apply the child height into setMeasuredDimension, and apparently fixed the height issue, but after playing a little bit, any change on the state breaks the position of the bottomSheet, so i had to call requestLayout for each state change through UIManager.dispatchViewManagerCommand, and works very well.

So, that's the implementation that fixes.

commit

BottomSheetBehaviorView.js

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    View child = this.getChildAt(0);

    if (child != null) {
        setMeasuredDimension(widthMeasureSpec, child.getHeight());
    }
}

BottomSheetBehaviorManager.js

@Override
public Map<String, Integer> getCommandsMap() {
    return MapBuilder.of("setRequestLayout", COMMAND_SET_REQUEST_LAYOUT);
}

@Override
public void receiveCommand(BottomSheetBehaviorView view, int commandType, @Nullable ReadableArray args) {
    if (commandType == COMMAND_SET_REQUEST_LAYOUT) {
        setRequestLayout(view);
    }
}

private void setRequestLayout(BottomSheetBehaviorView view) {
    view.requestLayout();
}

BottomSheetBehavior.js

  componentDidUpdate() {
    UIManager.dispatchViewManagerCommand(
      findNodeHandle(this),
      UIManager.RCTBottomSheetBehaviorAndroid.Commands.setRequestLayout,
      [],
    )
  }