一、安装依赖

官方文档

  1. yarn add react-navigation react-navigation-stack
  2. yarn add react-native-reanimated react-native-gesture-handler react-native-screens

1-1 请在android/app/build.gradle 中 dependencies 选项中添加下面这两行:

  1. implementation 'androidx.appcompat:appcompat:1.1.0-rc01'
  2. implementation 'androidx.swiperefreshlayout:swiperefreshlayout:1.1.0-alpha02'

1-2 请在 android/app/src/main/java/com/MainActivity.java 中做如下修改:

将+号后面的添加到对应位置

  1. package com.reactnavigation.example;
  2. import com.facebook.react.ReactActivity;
  3. + import com.facebook.react.ReactActivityDelegate;
  4. + import com.facebook.react.ReactRootView;
  5. + import com.swmansion.gesturehandler.react.RNGestureHandlerEnabledRootView;
  6. public class MainActivity extends ReactActivity {
  7. @Override
  8. protected String getMainComponentName() {
  9. return "Example";
  10. }
  11. + @Override
  12. + protected ReactActivityDelegate createReactActivityDelegate() {
  13. + return new ReactActivityDelegate(this, getMainComponentName()) {
  14. + @Override
  15. + protected ReactRootView createRootView() {
  16. + return new RNGestureHandlerEnabledRootView(MainActivity.this);
  17. + }
  18. + };
  19. + }
  20. }

1-3 index.js

  1. import 'react-native-gesture-handler'

1-4 启动项目连接模拟器