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!
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