12

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

可以像这样实现一个非常简单的 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);
    }
}

我的反应组件变成了这样。

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 组件但它不使用反应组件。那么,如何让一个RelativeLayoutto 包装一个 react<View style={{height: 300}} />组件呢?我也尝试实现一些度量 shadownode,但没​​有按预期工作,我不知道它们是如何工作的。

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

4

1 回答 1

3

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

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

犯罪

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

更新

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

  /**
   * 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;
  }

所以我修复了在 CoordinatorLayoutManager.java 上返回 true

这是它的样子。

反应原生底层行为

于 2016-08-16T11:46:30.980 回答