android中怎么利用ARouter实现组件化
这篇文章给大家介绍android中怎么利用ARouter实现组件化,内容非常详细,感兴趣的小伙伴们可以参考借鉴,希望对大家能有所帮助。
网站建设哪家好,找创新互联!专注于网页设计、网站建设、微信开发、小程序制作、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了阿克苏免费建站欢迎大家使用!
工程目录:
工程一共分为4个模块,基础组件app、基础服务(包涵路由服务)basecommonlibrary模块、业务模块libraryone、业务模块librarytwo;
在4个模块的gradle文件当中加入如下代码:
android { ... defaultConfig { ... javaCompileOptions { annotationProcessorOptions { arguments = [moduleName: project.getName()] } } } } dependencies { ... compile 'com.alibaba:arouter-api:1.2.1' annotationProcessor 'com.alibaba:arouter-compiler:1.1.2' }
依赖包的具体版本可自行选择。
接下来就是具体讲解ARouter如何在这几个模块中充当路由的角色了。
首先是app模块中定义的一个页面,布局很简单,就不贴代码了,效果如下;
Activity 部分代码如下:
@Override public void onClick(View v) { switch (v.getId()) { case R.id.bt1: RouterCommonUtil.startMainTextActivity(this, "测试文本"); break; case R.id.bt2: RouterCommonUtil.startMainImageActivity(this, "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1492584424522&di=5dcde77431914e0b944b8af9ff5f9277&imgtype=jpg&src=http%3A%2F%2Fimg3.imgtn.bdimg.com%2Fit%2Fu%3D1489971249%2C1618541505%26fm%3D214%26gp%3D0.jpg"); break; case R.id.bt3: RouterCommonUtil.startLibraryOneActivity(this); break; case R.id.bt4: RouterCommonUtil.startLibraryTwoActivity(this); break; } } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == 1002) { Toast.makeText(this, data.getStringExtra("arg2"), Toast.LENGTH_SHORT).show(); } }
点击事件分别响应到本module(app)中的两个activity以及libraryone和librarytwo中的activity,其中第一个用来测试ForResult方式启动的Acitivty。
接下来是libraryone中Activity布局截图:
activity部分代码如下:
@Override public void onClick(View v) { int i = v.getId(); if (i == R.id.bt1) { RouterCommonUtil.startMainActivity(this); } else if (i == R.id.bt2) { RouterCommonUtil.startLibraryTwoActivity(this); } }
也是测试了两个跳转,分别跳转到主Module中的Activity中和libraryTwo的Activity中,libraryTwo中的布局和代码跟libraryone中大同小异,这里就不继续贴出了,本项目会在附件中提供源码,欢迎下载。这里还需要贴出封装在basecommonlibrary模块中的公共跳转类,所有的module,都是通过该模块中的路由公共类来实现跳转的,完整代码如下:
import android.app.Activity; import android.text.TextUtils; import android.widget.Toast; import com.alibaba.android.arouter.facade.Postcard; import com.alibaba.android.arouter.launcher.ARouter; import com.example.basecommonlibrary.router.IInterceptor.InterruptCallback; import rx.Observable; import rx.Subscriber; import rx.android.schedulers.AndroidSchedulers; import rx.functions.Action1; /** * @author: xiewenliang * @Filename: * @Description: * @date: 2017/4/25 15:06 */ public class RouterCommonUtil { private static void toastInterruptInfo(final Activity activity, final Postcard postcard) { if (postcard.getTag() != null && postcard.getTag() instanceof String) { Observable.create(new Observable.OnSubscribe() { @Override public void call(Subscriber super String> subscriber) { subscriber.onNext((String) postcard.getTag()); } }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1 () { @Override public void call(String s) { if (!TextUtils.isEmpty(s) && activity != null) { Toast.makeText(activity, (String) postcard.getTag(), Toast.LENGTH_SHORT).show(); } } }); } } public static void startMainActivity(final Activity activity) { ARouter.getInstance().build("/ui/主页").navigation(activity, new InterruptCallback() { @Override public void onInterrupt(Postcard postcard) { toastInterruptInfo(activity, postcard); } }); } public static void startMainTextActivity(final Activity activity, String value) { ARouter.getInstance().build("/ui/text", "文本").withString("arg1", value).navigation(activity, 1001, new InterruptCallback() { @Override public void onInterrupt(Postcard postcard) { toastInterruptInfo(activity, postcard); } }); } public static void startMainImageActivity(final Activity activity, String value) { ARouter.getInstance().build("/ui/image", "图片").withString("arg1", value).navigation(activity, new InterruptCallback() { @Override public void onInterrupt(Postcard postcard) { toastInterruptInfo(activity, postcard); } }); } public static void startLibraryOneActivity(final Activity activity) { ARouter.getInstance().build("/libraryOne/主页").navigation(activity, new InterruptCallback() { @Override public void onInterrupt(Postcard postcard) { toastInterruptInfo(activity, postcard); } }); } public static void startLibraryTwoActivity(final Activity activity) { ARouter.getInstance().build("/libraryTwo/主页").navigation(activity, new InterruptCallback() { @Override public void onInterrupt(Postcard postcard) { toastInterruptInfo(activity, postcard); } }); } }
该类中主要提供了几个类的跳转方法,以及跳转失败后的信息打印方法,接下来就是测试跳转是否能够正常执行了,请大家自行构建代码或者下载本demo源码查看执行效果。
接下来讲解拦截器,以libraryone中的activity为例
@Route(path = "/libraryOne/主页", extras = CommonStation.CHECK_LOADING)
我们在activity上添加如上注解,其中extras的值可以作为判断的类型,具体看拦截器代码
import android.content.Context; import com.alibaba.android.arouter.facade.Postcard; import com.alibaba.android.arouter.facade.annotation.Interceptor; import com.alibaba.android.arouter.facade.callback.InterceptorCallback; import com.alibaba.android.arouter.facade.template.IInterceptor; import com.example.basecommonlibrary.CommonStation; @Interceptor(priority = 1, name = "拦截未登录") public class TestOneIInterceptor implements IInterceptor { @Override public void process(Postcard postcard, InterceptorCallback callback) { if (postcard.getExtra() == CommonStation.CHECK_LOADING && !CommonStation.checkLoading()) { callback.onInterrupt(new RuntimeException("账号未登录")); } else { callback.onContinue(postcard); } } @Override public void init(Context context) { } }
关于android中怎么利用ARouter实现组件化就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。
名称栏目:android中怎么利用ARouter实现组件化
新闻来源:http://scyanting.com/article/iipjeo.html