reactNative中的导航页面

reactNative中的导航页面


          reactNative中实现导航页面显示3秒后消失。

         

          reactNative中实现导航页面比android原生实现稍微麻烦

          需要涉及到reactNative JS DOM和原生android的交互


         实现思路:

         1.Android原生中实现Dialog的关闭和开启 的reactNative Moudle

         2.Android启动时在mainActity中开启Dialog

         3.Android启动后进入主页面,reactNative JS DOM 调用原生moudle 关闭Dialog


        我们实现导航页面显示3秒后消失需要引用第三方reactnative插件、Dialog的关闭和开启Android、IOS第三方插件

        具体实现思路和上面说到的实现思路一致。

 

        引用插件步奏

        npm install react-native-splash-screen --save   //Dialog的关闭和开启第三方插件

        react-native link react-native-splash-screen        //通过rn导入安卓ios依赖包


        实现思路图:

      

     实现代码:

    android部分:

    reactNativeModule

    SplashScreenModule.java:

public class SplashScreenModule extends ReactContextBaseJavaModule{
    public SplashScreenModule(ReactApplicationContext reactContext) {
        super(reactContext);
    }

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

    /**
     * 打开启动屏
     */
    @ReactMethod
    public void show() {
        SplashScreen.show(getCurrentActivity());
    }

    /**
     * 关闭启动屏
     */
    @ReactMethod
    public void hide() {
        SplashScreen.hide(getCurrentActivity());
    }
}

     Dialog开启和关闭:

     SplashScreen.java:

public class SplashScreen {
    private static Dialog mSplashDialog;
    private static WeakReference<Activity> mActivity;

    /**
     * 打开启动屏
     */
    public static void show(final Activity activity,final boolean fullScreen) {
        if (activity == null) return;
        mActivity = new WeakReference<Activity>(activity);
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (!activity.isFinishing()) {

                    mSplashDialog = new Dialog(activity,fullScreen? R.style.SplashScreen_Fullscreen:R.style.SplashScreen_SplashTheme);
                    mSplashDialog.setContentView(R.layout.launch_screen);
                    mSplashDialog.setCancelable(false);

                    if (!mSplashDialog.isShowing()) {
                        mSplashDialog.show();
                    }
                }
            }
        });
    }
    /**
     * 打开启动屏
     */
    public static void show(final Activity activity) {
        show(activity,false);
    }

    /**
     * 关闭启动屏
     */
    public static void hide(Activity activity) {
        if (activity == null) activity = mActivity.get();
        if (activity == null) return;

        //让该Active的主线程关闭Dialog
        activity.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if (mSplashDialog != null && mSplashDialog.isShowing()) {
                    mSplashDialog.dismiss();
                }
            }
        });
    }
}


        将Dialog moudle方法 moudle包中:

       SplashScreenReactPackage.java:

public class SplashScreenReactPackage implements ReactPackage {

    @Override
    public List<Class<? extends JavaScriptModule>> createJSModules() {
        return Collections.emptyList();
    }

    @Override
    public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
        return Collections.emptyList();
    }

    @Override
    public List<NativeModule> createNativeModules(
            ReactApplicationContext reactContext) {
        List<NativeModule> modules = new ArrayList<>();
        modules.add(new SplashScreenModule(reactContext));
        return modules;
    }
}

     MainApplication中添加mudle包:

     MainApplication.java:

public class MainApplication extends Application implements ReactApplication {

  private final ReactNativeHost mReactNativeHost = new ReactNativeHost(this) {
    @Override
    public boolean getUseDeveloperSupport() {
      return BuildConfig.DEBUG;
    }

    @Override
    protected List<ReactPackage> getPackages() {
      return Arrays.<ReactPackage>asList(
          new MainReactPackage(),
              new SplashScreenReactPackage()
      );
    }
  };

  @Override
  public ReactNativeHost getReactNativeHost() {
    return mReactNativeHost;
  }

  @Override
  public void onCreate() {
    super.onCreate();
    SoLoader.init(this, /* native exopackage */ false);
  }
}

     MainActity中开启Dialog:

     MainActity.java:

public class MainActivity extends ReactActivity {

    /**
     * Returns the name of the main component registered from JavaScript.
     * This is used to schedule rendering of the component.
     */
    @Override
    protected String getMainComponentName() {
        return "reactnativedemo";
    }

     @Override
     protected void onCreate(Bundle savedInstanceState) {
         //先显示欢迎界面
         SplashScreen.show(this);
         super.onCreate(savedInstanceState);
     }
}


      ReactNative  JS部分:

     HomePage.js:

 componentDidMount(){
        //关闭欢迎页面
        SplashScreen.hide();

        //添加事件监听
        this.listener=DeviceEventEmitter.addListener('HOMEPAGE_RELOAD',(n)=>{
            //主页重新加载
            //跳转到新的场景,并且重置整个路由栈
            this.props.navigator.resetTo({
               component:HomePage
            });

            JasonToast.show("123HOMEPAGE_RELOAD",JasonToast.LONG);

        });
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

慕容屠苏

你的鼓励是我最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值