|
1 | 1 | # android组件化工程
|
2 | 2 |
|
3 |
| -本工程使用现在最流行的组件化方案,多 module 方案,并使用 aRouter 作为支撑。 |
| 3 | +本工程使用的是多model合一的组件化方案,并使用 aRouter 作为支撑,框架灵活、业务和功能分离。 |
4 | 4 |
|
5 | 5 | 
|
6 | 6 |
|
7 | 7 |
|
8 |
| -# liveDataBus |
| 8 | +由于本工程中使用的是: |
| 9 | +```aidl |
| 10 | +implementation project(path: ':common') |
| 11 | +``` |
| 12 | +方式引入module,如果之后的业务需求增多,可能会导致**module**数量增多,app可能会引用多个**module**,导致编译速度变慢。可以把组件module生成aar形式上传 |
| 13 | +到maven库,或公司的私有maven库上。使用的时候只需要 依赖 一下就可以了。 |
| 14 | +* [android studio生产aar](https://blog.csdn.net/u011511601/article/details/80579543) |
| 15 | +* [aar上传maven](https://blog.csdn.net/a568478312/article/details/80166281) |
| 16 | + |
| 17 | + |
| 18 | +## ARouter |
| 19 | + |
| 20 | +自从阿里[ARouter](https://github.com/alibaba/ARouter)框架开源以来,model间不在有更多的耦合,android的mvp、mvvm框架也变成了其中的一部分。组件化后, |
| 21 | +mvp和mvvm都变得可以随意替换或共存。团队协作也不再变得嘈杂。具体原理可以 [点击](https://github.com/alibaba/ARouter) |
| 22 | + |
| 23 | +在工程common中的**ARouterManager.class**中,有统一写跳转方式和跳转动画。 |
| 24 | + |
| 25 | + |
| 26 | +```aidl |
| 27 | + /** |
| 28 | + * 不带参数的跳转 |
| 29 | + * @param url |
| 30 | + */ |
| 31 | + public static void startActivity(Activity activity,String url){ |
| 32 | + ARouter.getInstance() |
| 33 | + .build(url) |
| 34 | + .withTransition(startTransition(0,Constant.AnimationType.LEFT),startTransition(1,Constant.AnimationType.LEFT)) |
| 35 | + .navigation(activity); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * 带参数跳转 |
| 40 | + * @param url 跳转的路由地址 |
| 41 | + * @param bundle 携带的参数 |
| 42 | + */ |
| 43 | + public static void startActivity(Activity activity,String url, Bundle bundle){ |
| 44 | + // 对象传递 |
| 45 | + ARouter.getInstance() |
| 46 | + .build(url) |
| 47 | + .with(bundle) |
| 48 | + .withTransition(startTransition(0,Constant.AnimationType.LEFT),startTransition(1,Constant.AnimationType.LEFT)) |
| 49 | + .navigation(activity); |
| 50 | + } |
| 51 | +``` |
| 52 | + |
| 53 | +同时路由参数可以在module中统一配置: |
| 54 | + |
| 55 | +```aidl |
| 56 | +/** |
| 57 | + * 描述: 本工程的所有路由跳转,统一维护 |
| 58 | + * 作者 : Tong |
| 59 | + * e-mail : itangbei@sina.com |
| 60 | + * 创建时间: 2019/9/11. |
| 61 | + */ |
| 62 | +public class CommonRouter { |
| 63 | +
|
| 64 | + /** |
| 65 | + * app |
| 66 | + */ |
| 67 | + public static final String PATH_APP_SPLASH_ACTIVITY = "/app/SplashActivity"; |
| 68 | +
|
| 69 | + public static final String PATH_APP_TEST_ACTIVITY = "/app/TestActivity"; |
| 70 | +
|
| 71 | + /** |
| 72 | + * main |
| 73 | + */ |
| 74 | + public static final String PATH_APP_MAIN_ACTIVITY = "/main/MainActivity"; |
| 75 | +
|
| 76 | + public static final String PATH_APP_TESTS_ACTIVITY = "/main/TestActivity"; |
| 77 | +
|
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +也可以统一处理跳转拦截等: |
| 82 | + |
| 83 | +```aidl |
| 84 | +/** |
| 85 | + * 描述: 登录拦截 |
| 86 | + * 作者 : Tong |
| 87 | + * e-mail : itangbei@sina.com |
| 88 | + * 创建时间: 2019/9/17. |
| 89 | + */ |
| 90 | +@Interceptor(priority = 8, name = "登录拦截器") |
| 91 | +public class LoginInterceptor implements IInterceptor { |
| 92 | +
|
| 93 | + @Override |
| 94 | + public void process(Postcard postcard, InterceptorCallback callback) { |
| 95 | + LogUtil.d("tang--->","我是登录拦截器哦"); |
| 96 | + callback.onContinue(postcard); // 处理完成,交还控制权 |
| 97 | + } |
| 98 | +
|
| 99 | + @Override |
| 100 | + public void init(Context context) { |
| 101 | + LogUtil.d("tang--->","我是登录拦截器哦init"); |
| 102 | + } |
| 103 | +} |
| 104 | +
|
| 105 | +``` |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | +## liveDataBus |
9 | 110 |
|
10 | 111 | 项目中添加了liveDataBus,果断抛弃eventBus。有太多优点了,轻量、便于理解。
|
11 | 112 | 具体可以 点击 [liveDataBus](https://juejin.im/post/5b5ac0825188251acd0f3777)
|
|
0 commit comments