我正在编写一个本地的 Android 模块,它包装了一个 BottomSheetBehavior。
一个非常简单的 BottomSheetBehavior 可以像这样实现: https://gist.github.com/cesardeazevedo/a4dc4ed12df33fe1877fc6cea42475ae
我面临的第一个问题是整个页面必须是 CoordinatorLayout 的子元素,并在其末尾添加 BottomSheetBehavior。
所以我不得不编写两个本地模块:<CoordinatorLayout />
和 <BottomSheetBehavior />
。
这是 BottomSheetBehavior 包装器。
BottomSheetBehaviorManager.java
public class BottomSheetBehaviorManager extends ViewGroupManager<BottomSheetBehaviorView> {
@Override
public BottomSheetBehaviorView createViewInstance(ThemedReactContext context) {
return new BottomSheetBehaviorView(context);
}
}
底部工作表行为视图.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);
}
}
我的React组件变成了这样。
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>
)
它有效!
但是我一直在努力使BottomSheet包裹其子视图并使用wrap_content
,它不应该滑动整个屏幕,而应该只能通过包装的内容(在这种情况下是lorem ipsum文本)滑动,它可以与android组件一起使用,但不能与react组件一起使用。那么,如何使RelativeLayout
包裹一个react <View style={{height: 300}} />
组件?我还尝试过实现一些测量阴影节点,但效果不如预期,我不知道它们是如何工作的。
我已经将此示例添加到我的GitHub上供大家尝试。 https://github.com/cesardeazevedo/react-native-bottom-sheet-behavior
BottomSheetFragment
并提交PR! :) - Noitidart