首页 > 解决方案 > 如何禁用滑动打开但不关闭反应原生?

问题描述

我想禁用滑动以打开抽屉导航但不关闭?

我找到了 android 的解决方案,但没有找到 react-native 的解决方案。目前我正在使用这个:

drawerLockMode: 'locked-open'

哪个有效,但我无法通过滑动关闭它。

我用这样的汉堡菜单图标打开抽屉:

onPress={() => props.navigation.toggleDrawer()}

这里有人已经这样做了吗?有没有办法drawerLockMode全局设置?onDrawerOpenedonDrawerClosed道具存在吗?谢谢!

标签: react-nativereact-native-navigationreact-native-drawer

解决方案


你需要一个react-native-gesture-handler模块来swipe.

你可以跑

 yarn add react-native-gesture-handler

OR

npm install --save react-native-gesture-handler

react-native link react-native-gesture-handler

更新您的MainActivity.java文件(或您创建 的实例的任何位置ReactActivityDelegate),以便它覆盖负责创建ReactRootView实例的方法,然后使用此库提供的根视图包装器。不要忘记导入ReactActivityDelegateReactRootViewRNGestureHandlerEnabledRootView

package com.swmansion.gesturehandler.react.example;

import com.facebook.react.ReactActivity;
+ import com.facebook.react.ReactActivityDelegate;
+ import com.facebook.react.ReactRootView;
+ import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;

public class MainActivity extends ReactActivity {

  @Override
  protected String getMainComponentName() {
    return "Example";
  }

+  @Override
+  protected ReactActivityDelegate createReactActivityDelegate() {
+    return new ReactActivityDelegate(this, getMainComponentName()) {
+      @Override
+      protected ReactRootView createRootView() {
+       return new RNGestureHandlerEnabledRootView(MainActivity.this);
+      }
+    };
+  }
}

有关详细信息,请参阅


推荐阅读