【发布时间】:2016-12-19 17:10:03
【问题描述】:
我一直在编写一个原生 android 模块,它封装了 BottomSheetBehavior。
一个非常简单的BottomSheetBehavior可以这样实现 https://gist.github.com/cesardeazevedo/a4dc4ed12df33fe1877fc6cea42475ae
我面临的第一件事是,整个页面必须是 CoordinatorLayout 的子级,并且底部是 BottomSheetBehavior。
所以我不得不编写 2 个原生模块,<CoordinatorLayout /> 和 <BottomSheetBehavior />。
这是 bottomSheetBehavior 包装器。
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);
}
}
而我的 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 来包装一个反应 <View style={{height: 300}} /> 组件?我也尝试实现了一些measure shadownode,但没有按预期工作,我不知道它们是如何工作的。
我已经在我的 github 上添加了这个示例,供大家尝试。 https://github.com/cesardeazevedo/react-native-bottom-sheet-behavior
【问题讨论】:
标签: android react-native react-native-android bottom-sheet