Android

关注公众号 jb51net

关闭
首页 > 软件编程 > Android > android组件化 ARouter

浅谈android组件化之ARouter简单使用

作者:繁华落尽666

本篇文章主要介绍了浅谈android组件化之ARouter简单使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

ARouter是阿里巴巴开源出来的一款android路由框架,github地址为 : https://github.com/alibaba/ARouter

至于ARouter的诸多好处我就不介绍了,这里主要讲解在项目组件化下,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<String>() { 
        @Override 
        public void call(Subscriber<? super String> subscriber) { 
          subscriber.onNext((String) postcard.getTag()); 
        } 
      }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Action1<String>() { 
        @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; 
 
/** 
 * @author: xiewenliang 
 * @Filename: 
 * @Description: 
 * @date: 2017/4/25 15:37 
 */ 
@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) { 
 
  } 
} 

在拦截器中,可以根据extra属性判断是否是需要拦截的类型,如果需要拦截,则执行callback的onInterrupt方法,并注明拦截原因,可以在路由跳转中提取拦截原因信息并打印。

当然ARouter中其它功能就不再一一介绍了,大家可以自行研究,附源码下载地址:ARouterDemo_jb51.rar

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文