gpt4 book ai didi

android - CoordinatorLayout 和 BottomSheetBehavior 的 react-native 包装器

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:46:28 26 4
gpt4 key购买 nike

我一直在编写一个包含 BottomSheetBehavior 的原生 android 模块.

一个非常简单的 BottomSheetBehavior 可以这样实现 https://gist.github.com/cesardeazevedo/a4dc4ed12df33fe1877fc6cea42475ae

我遇到的第一件事是,整个页面必须是 CoordinatorLayout 的子项和它末尾的 BottomSheetBehavior。

所以我必须编写 2 个 native 模块,<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>
)

而且有效!

react-native-bottomsheet-behavior

但我一直在努力让 BottomSheet 用 wrap_content 包裹他们的 child ,它不应该滑动整个屏幕,它应该只滑动包装的内容(在本例中是 lorem ipsum 文本),它适用于 android 组件但不适用于 react 组件。那么,如何制作 RelativeLayout包装 react <View style={{height: 300}} />零件?我也尝试实现一些 measure shadownode , 但没有按预期工作,我不知道它们是如何工作的。

我已经在我的 github 上添加了这个示例,供每个想尝试的人使用。 https://github.com/cesardeazevedo/react-native-bottom-sheet-behavior

最佳答案

经过大量调试,终于明白了,我必须做两件事,首先是覆盖 onMeasure 函数并将子高度应用到 setMeasuredDimension 中,显然修复了高度问题,但是玩了一会儿之后,状态的任何变化都会破坏 bottomSheet 的位置,所以我不得不通过 UIManager.dispatchViewManagerCommand 为每个状态变化调用 requestLayout,并且效果很好。

所以,这就是修复的实现。

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,
[],
)
}

更新

我意识到滑动时更新状态会闪烁所有布局,在查看一些库代码后,我找到了 needsCustomLayoutForChildren 函数,该函数在 ViewGroupManager.java 中有描述

  /**
* Returns whether this View type needs to handle laying out its own children instead of
* deferring to the standard css-layout algorithm.
* Returns true for the layout to *not* be automatically invoked. Instead onLayout will be
* invoked as normal and it is the View instance's responsibility to properly call layout on its
* children.
* Returns false for the default behavior of automatically laying out children without going
* through the ViewGroup's onLayout method. In that case, onLayout for this View type must *not*
* call layout on its children.
*/
public boolean needsCustomLayoutForChildren() {
return false;
}

所以我fixed在 CoordinatorLayoutManager.java 上返回 true

这是它的样子。

react-native-bottom-sheet-behavior

关于android - CoordinatorLayout 和 BottomSheetBehavior 的 react-native 包装器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38922156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com