[Android]大佬们,如何在原生Android的fragment调react-native的页面?



  • 0_1504519009784_12312321321.png

    如图:
    整个activity(有三个教练、首页、我的tab),tab里都是fragment,我现在想在fragment中调rn的页面,Google和百度出的教程,弄出来,现在能启动rn页面但是,一直都是“welcome。。。”这个页面,然后还不能打开开发者菜单进行reload。已经不清楚怎么弄了呢。。。。

    贴代码:
    / MainActivity.java /
    package com.rnapp.NActivity;

    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.support.v4.app.FragmentPagerAdapter;
    import android.support.v4.view.ViewPager;
    import android.support.v7.app.AppCompatActivity;
    import android.view.KeyEvent;
    import android.view.View;
    import android.view.Window;
    import android.widget.Button;
    import android.widget.LinearLayout;

    import com.facebook.react.ReactInstanceManager;
    import com.facebook.react.ReactRootView;
    import com.facebook.react.common.LifecycleState;
    import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
    import com.rnapp.MainApplication;
    import com.rnapp.NFrag.CoachFragment;
    import com.rnapp.NFrag.MainFragment;
    import com.rnapp.NFrag.ProfileFragment;
    import com.rnapp.R;

    import java.util.ArrayList;
    import java.util.List;

    /**

    • Created by Administrator on 2017/9/1.
      */

    public class MainActivity extends AppCompatActivity implements DefaultHardwareBackBtnHandler, View.OnClickListener {
    /RN需要的/
    private ReactRootView mReactRootView;
    private ReactInstanceManager mReactInstanceManager;
    private LifecycleState mLifecycleState
    = LifecycleState.BEFORE_RESUME;

    //声明ViewPager
    private ViewPager viewPager;
    //适配器
    private FragmentPagerAdapter fragmentPagerAdapter;
    //装载Fragment的集合
    private List<Fragment> fragmentList;
    //3个Tab对应的布局
    private LinearLayout tabCoach;
    private LinearLayout tabMain;
    private LinearLayout tabProfile;
    //3个Tab对应的Button
    private Button buttonCoach;
    private Button buttonMain;
    private Button buttonProfile;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        /*RN*/
        mReactInstanceManager =  ((MainApplication) getApplication()).getReactNativeHost().getReactInstanceManager();
        /*----------------------------*/
        //窗体属性
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.main_layout);
        initViews();
        initDatas();
        initEvents();
    }
    /*点击事件*/
    @Override
    public void onClick(View v) {
    
        resetButStyle();
    
        switch (v.getId()) {
            case R.id.id_tab_coach_lay:
                selectTab(0);
                break;
            case R.id.id_tab_main_lay:
                selectTab(1);
                break;
            case R.id.id_tab_profile_lay:
                selectTab(2);
                break;
        }
    }
    /*初始化控件*/
    private void initViews(){
        viewPager = (ViewPager) findViewById(R.id.id_viewpager);
    
        tabCoach = (LinearLayout) findViewById(R.id.id_tab_coach_lay);
        tabMain = (LinearLayout) findViewById(R.id.id_tab_main_lay);
        tabProfile = (LinearLayout) findViewById(R.id.id_tab_profile_lay);
    
        buttonCoach = (Button) findViewById(R.id.id_tab_coach_but);
        buttonMain = (Button) findViewById(R.id.id_tab_main_but);
        buttonProfile = (Button) findViewById(R.id.id_tab_profile_but);
    
    }
    /*点击事件初始化*/
    private void initEvents(){
        //设置四个Tab的点击事件
        tabCoach.setOnClickListener(this);
        tabMain.setOnClickListener(this);
        tabProfile.setOnClickListener(this);
    }
    /*初始化数据、处理逻辑*/
    private void initDatas(){
        /*加入碎片*/
        fragmentList = new ArrayList<>();
        fragmentList.add(new CoachFragment());
        fragmentList.add(new MainFragment());
        fragmentList.add(new ProfileFragment());
    
        /*初始化适配器*/
        fragmentPagerAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return fragmentList.get(position);
            }
    
            @Override
            public int getCount() {
                return fragmentList.size();
            }
        };
        /*设置适配器*/
        viewPager.setAdapter(fragmentPagerAdapter);
        /*设置viewPager的切换监听*/
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            /*页面滚动事件*/
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
            /*页面选中事件*/
            @Override
            public void onPageSelected(int position) {
                viewPager.setCurrentItem(position);
                resetButStyle();
                selectTab(position);
            }
            /*页面滚动状态事件*/
            @Override
            public void onPageScrollStateChanged(int state) {}
        });
        viewPager.setCurrentItem(1);//设置默认
    }
    /*选中layout的按钮样式改变*/
    private void selectTab(int i) {
        switch (i){
            case 0:
                buttonCoach.setTextSize(24);
                buttonCoach.setTextColor(android.graphics.Color.parseColor("#FF8000"));
                break;
            case 1:
                buttonMain.setTextSize(24);
                buttonMain.setTextColor(android.graphics.Color.parseColor("#FF8000"));
                break;
            case 2:
                buttonProfile.setTextSize(24);
                buttonProfile.setTextColor(android.graphics.Color.parseColor("#FF8000"));
                break;
        }
        viewPager.setCurrentItem(i);
    
    }
    /*重置按钮的样式*/
    private void resetButStyle(){
        buttonCoach.setTextSize(18);
        buttonCoach.setTextColor(android.graphics.Color.parseColor("#8E8E8E"));
        buttonMain.setTextSize(18);
        buttonMain.setTextColor(android.graphics.Color.parseColor("#8E8E8E"));
        buttonProfile.setTextSize(18);
        buttonProfile.setTextColor(android.graphics.Color.parseColor("#8E8E8E"));
    }
    
    @Override
    protected void onPause() {
        super.onPause();
    
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostPause();
        }
    }
    
    @Override
    protected void onResume() {
        super.onResume();
    
        if (mReactInstanceManager != null) {
            mReactInstanceManager.onHostResume(this, this);
        }
    }
    
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) {
            mReactInstanceManager.showDevOptionsDialog();
            return true;
        }
        return super.onKeyUp(keyCode, event);
    }
    
    @Override
    public void invokeDefaultOnBackPressed() {
        super.onBackPressed();
    }
    

    }

    / ReactFragment.java /

    package com.rnapp.NFrag;

    import android.content.Context;
    import android.os.Bundle;
    import android.support.annotation.Nullable;
    import android.support.v4.app.Fragment;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    import com.facebook.react.ReactInstanceManager;
    import com.facebook.react.ReactRootView;
    import com.facebook.react.modules.core.DefaultHardwareBackBtnHandler;
    import com.rnapp.MainApplication;

    /**

    • Created by Administrator on 2017/9/1.
      */

    public abstract class ReactFragment extends Fragment {
    private ReactRootView reactRootView;
    private ReactInstanceManager reactInstanceManager;

    public abstract String getMainComponentName();
    
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        reactRootView = new ReactRootView(context);
        reactInstanceManager = ((MainApplication) getActivity().getApplication()).getReactNativeHost().getReactInstanceManager();
    }
    
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        return reactRootView;
    }
    
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        reactRootView.startReactApplication(reactInstanceManager, getMainComponentName(), null);
    }
    
    @Override
    public void onResume() {
        super.onResume();
        reactInstanceManager.onHostResume(getActivity(), (DefaultHardwareBackBtnHandler) getActivity());
    }
    

    }

    / MainFragment.java /

    package com.rnapp.NFrag;

    /**

    • Created by Administrator on 2017/9/4.
      */

    public class MainFragment extends ReactFragment {
    @Override
    public String getMainComponentName() {
    return "RNApp";
    }
    }

    / MainApplication.java /

    package com.rnapp;

    import android.app.Application;

    import com.facebook.react.BuildConfig;
    import com.facebook.react.ReactApplication;
    import com.facebook.react.ReactNativeHost;
    import com.facebook.react.ReactPackage;
    import com.facebook.react.bridge.ReactContext;
    import com.facebook.react.shell.MainReactPackage;

    import java.util.Arrays;
    import java.util.List;

    public class MainApplication extends Application implements ReactApplication {
    private ReactContext mReactContext;

    public ReactContext getReactContext() {
    return mReactContext;
    }

    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()
      );
    }
    

    };

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

    }



  • 双击R或者使用命令:adb shell input keyevent 82



  • @JxSx 大佬,双击R或者Ctrl + M呼不出开发者菜单,完全没反应的那种呢



  • @A-DoubleCheng 命令行也不好使吗?那就不清楚了


登录后回复