【问题标题】:Let touches pass through in React Native Android embedded fragment?让触摸在 React Native Android 嵌入式片段中通过?
【发布时间】:2021-06-18 14:45:57
【问题描述】:

我有一个嵌入了 React Native 应用程序的片段,该应用程序在原生片段上加载视图。我的观点在两个移动平台上都呈现,并且在 iOS 上完美运行。我能够激活一个名为 RCTRootView.passThroughTouches

的属性

有没有办法让触摸通过视图与后面的原生视图交互?

我的片段是按照https://reactnative.dev/docs/integration-with-android-fragment 的步骤创建的,并像这样添加到我的布局中:

val reactNativeFragment = ReactFragment.Builder()
    .setComponentName("MainFABActionsContainer")
            .build()

    childFragmentManager
            .beginTransaction()
            .add(R.id.fab_actions, reactNativeFragment)
            .commit()

我的 XML 中的框架布局如下所示:

    <FrameLayout
        android:id="@+id/fab_actions"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

谢谢!

【问题讨论】:

    标签: android ios react-native android-fragments


    【解决方案1】:

    已经很久了,但我还是回答一下,以防将来有人遇到这个问题。问题的原因是 React Native 在自定义视图调用 ReactRootView 中绘制所有内容。该视图将实现onTouchEvent 并返回true,这意味着它将处理触摸事件,而不是将其分派给下面的所有视图。为了解决这个问题,我覆盖了根视图的 onTouchEvent 并将返回值更改为 false

    所以步骤是

    • 创建CustomReactFragment 喜欢this
    • 创建CustomReactDelegate 喜欢this
    • 创建CustomReactRootView 喜欢this。请注意,在CustomReactRootView 中,我扩展了RNGestureHandlerEnabledRootView,因为我使用React Native getsure handler 作为我的底部工作表,您可以将其更改为ReactRootView

    现在你可以用CustomReactFragment.Builder() 替换ReactFragment.Builder() 来启动你的React Native Fragment。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 1970-01-01
      • 2022-06-16
      • 2016-12-21
      相关资源
      最近更新 更多