如何从React Native获取Android应用程序的根视图?

时间:2015-11-17 08:24:25

标签: android view react-native

尝试为Flipboard bottomsheet component编写react-native native适配器。

我可以将他们的BottomSheetLayout实例化,但无法尝试使用视图进行设置。

Per API docs我正在尝试使用“正在显示的工作表下显示的视图”({通常是应用程序的根视图)调用setContentView,但我似乎无法获得我的React课程中的正确视图。

尝试保存对ReactRootView中实例化的MainActivity的引用,但它被拒绝,“指定的子项已经有父项”。

我还缺少React Native应用程序的另一个根视图吗?如何从我的适配器类中获取它的引用?

我的组件管理器类,简化:

public class ReactBottomSheetManager extends ViewGroupManager<ReactBottomSheetLayout> {
  private View mRootView = null;
  private View mContentView = null;

  public static final int COMMAND_OPEN = 1;

  public ReactBottomSheetManager(View rootView) {
    super();
    mRootView = rootView; // this is the ReactRootView from MainActivity
  }

  @Override
  public String getName() {
    return "BottomSheetAndroid";
  }

  @Override
  protected ReactBottomSheetLayout createViewInstance(ThemedReactContext context) {
    ReactBottomSheetLayout view = new ReactBottomSheetLayout(context);
    view.setPeekOnDismiss(true);
    view.setContentView(mRootView);  // here is where things blow up
    return view;
  }

  @Override
  public Map<String, Integer> getCommandsMap() {
    return MapBuilder.of("open", COMMAND_OPEN);
  }

  @Override
  public void receiveCommand(ReactBottomSheetLayout view, int commandType, @Nullable ReadableArray args) {
    switch(commandType) {
      case COMMAND_OPEN: {
        view.showWithSheetView(mContentView);
        return;
      }
    }
  }

  // overriding addView to save a reference to the view instead of adding it
  // to the bottom sheet layout right away. we'll add/show this view when
  // it's time to open the sheet.
  @Override
  public void addView(ReactBottomSheetLayout parent, View child, int index) {
    Log.d("ReactNative", "addView called with index=" + index);
    mContentView = child;
  }

  @Override
  public boolean needsCustomLayoutForChildren() {
    // BottomSheetLayout will lay out it's own children
    return true;
  }
}

ReactBottomSheetLayout是一个非常简单的类,继承了Flipboard的BottomSheetLayout

谢谢!

0 个答案:

没有答案