您好,登錄后才能下訂單哦!
這篇文章給大家介紹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<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; @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實現組件化就分享到這里了,希望以上內容可以對大家有一定的幫助,可以學到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。