为了账号安全,请及时绑定邮箱和手机立即绑定

阿里ARouter路由实现模块化开发流程(二)

标签:
Android

使用阿里ARouter路由实现模块化开发流程(一)

ARouter初始化

在lib_base中创建MyApplication

public class MyApplication extends Application {
    /**
     * 上下文
     */
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        initRouter(this);
    }

    public static Context getInstance() {
        return instance;
    }

    private void initRouter(MyApplication mApplication) {
        // 这两行必须写在init之前,否则这些配置在init过程中将无效
        if (UIUtils.isApkInDebug(instance)) {
            //打印日志
            ARouter.openLog();
            //开启调试模式(如果在InstantRun模式下运行,必须开启调试模式!
            //线上版本需要关闭,否则有安全风险)
            ARouter.openDebug();
        }
        // 尽可能早,推荐在Application中初始化
        ARouter.init(mApplication);

    }
}
 /**
     * 判断当前应用是否是debug状态
     */

    public static boolean isApkInDebug(Context context) {
        try {
            ApplicationInfo info = context.getApplicationInfo();
            return (info.flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0;
        } catch (Exception e) {
            return false;
        }
    }

这里要注意:如果你在debug模式下进行调试代码,ARouter.openDebug();一定要调用,否则ARouter会出现无效情况。

首页的搭建

图片描述

使用ARouter来获取到各个模块的Fragment

package tsou.cn.lib_base.utils;

/**
 * Created by Administrator on 2017/12/4 0004.
 */

public class RouteUtils {
    //获得home模块fragment
    public static final String Home_Fragment_Main = "/home/main";
    //获得chat模块fragment
    public static final String Chat_Fragment_Main = "/chat/main";
    //获得Recom模块fragment
    public static final String Recom_Fragment_Main = "/recom/main";
    //获得Me模块fragment
    public static final String Me_Fragment_Main = "/me/main";
    //跳转到登录页面
    public static final String Me_Login = "/me/main/login";
    //跳转到eventBus数据接收页面
    public static final String Me_EventBus = "/me/main/EventBus";
    //跳转到TextOne数据接收页面
    public static final String Me_TextOne = "/me/main/TextOne";
    //跳转到Test公用页面
    public static final String Me_Test = "/me/main/Test";
    //路由拦截
    public static final String Me_Test2 = "/me/main/Test2";
    //跳转到webview页面
    public static final String Me_WebView = "/me/main/WebView";

    //跳转到依赖注入页面
    public static final String Me_Inject = "/me/main/Inject";
    /**
     * 依赖注入使用,注意:必须实现SerializationService进行注册,
     */
    public static final String Home_Json_Service = "/huangxiaoguo/json";

    //跳转ForResult
    public static final String Chat_ForResult = "/chat/main/ForResult";
    //模块间服务调用,调用chat模块的接口
    public static final String Service_Chat = "/chat/service";
    //路由拦截
    public static final String Chat_Interceptor = "/chat/main/Interceptor";

    /**
     * 专门的分组,这里就叫做needLogin组,凡是在这个组下的,都会进行登录操作
     */
    public static final String NeedLogin_Test3 = "/needLogin/main/Test3";

}
package tsou.cn.myaroutertest.utils;

import android.support.v4.app.Fragment;

import com.alibaba.android.arouter.launcher.ARouter;

import tsou.cn.lib_base.utils.RouteUtils;

/**
 * Created by Administrator on 2017/12/4 0004.
 */

public class FragmentUtils {

    public static Fragment getHomeFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouteUtils.Home_Fragment_Main).navigation();
        return fragment;
    }

    public static Fragment getChatFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouteUtils.Chat_Fragment_Main).navigation();
        return fragment;
    }

    public static Fragment getRecomFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouteUtils.Recom_Fragment_Main).navigation();
        return fragment;
    }

    public static Fragment getMeFragment() {
        Fragment fragment = (Fragment) ARouter.getInstance().build(RouteUtils.Me_Fragment_Main).navigation();
        return fragment;
    }
}
package tsou.cn.myaroutertest;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

import tsou.cn.myaroutertest.utils.FragmentUtils;

public class MainActivity extends AppCompatActivity {

    private ViewPager mMViewPager;
    private TabLayout mToolbarTab;
    /**
     * 图标
     */
    private int[] tabIcons = {
            R.drawable.tab_home,
            R.drawable.tab_weichat,
            R.drawable.tab_recommend,
            R.drawable.tab_user
    };
    private String[] tab_array;
    private DemandAdapter mDemandAdapter;
    private ArrayList<Fragment> fragments = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initData();
        initView();
//         给viewpager设置适配器
        setViewPagerAdapter();
        setTabBindViewPager();
        setItem();
    }

    private void initData() {
        tab_array = getResources().getStringArray(R.array.tab_main);
        fragments.clear();
        fragments.add(FragmentUtils.getHomeFragment());
        fragments.add(FragmentUtils.getChatFragment());
        fragments.add(FragmentUtils.getRecomFragment());
        fragments.add(FragmentUtils.getMeFragment());
    }

    private void initView() {
        mMViewPager = (ViewPager) findViewById(R.id.mViewPager);
        mToolbarTab = (TabLayout) findViewById(R.id.toolbar_tab);

    }

    private void setViewPagerAdapter() {
        mDemandAdapter = new DemandAdapter(getSupportFragmentManager());
        mMViewPager.setAdapter(mDemandAdapter);
    }

    private void setTabBindViewPager() {
        mToolbarTab.setupWithViewPager(mMViewPager);
    }

    private void setItem() {
        /**
         * 一定要在设置适配器之后设置Icon
         */
        for (int i = 0; i < mToolbarTab.getTabCount(); i++) {
            mToolbarTab.getTabAt(i).setCustomView(getTabView(i));
        }
    }

    public View getTabView(int position) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_tab, null);
        ImageView tab_image = view.findViewById(R.id.tab_image);
        TextView tab_text = view.findViewById(R.id.tab_text);
        tab_image.setImageResource(tabIcons[position]);
        tab_text.setText(tab_array[position]);
        return view;
    }

    /**
     * 适配器
     */
    public class DemandAdapter extends FragmentStatePagerAdapter {

        public DemandAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {

            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

    }
}

代码中有注释,切很简单,不会出现什么毛病!如果有小毛病,就clean一下解决.

使用阿里ARouter路由实现模块化开发流程(三)

点击查看更多内容
4人点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
移动开发工程师
手记
粉丝
50
获赞与收藏
188

关注作者,订阅最新文章

阅读免费教程

感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消