這篇文章給大家介紹android中怎么利用ARouter實現(xiàn)組件化,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。
網(wǎng)站建設(shè)哪家好,找創(chuàng)新互聯(lián)!專注于網(wǎng)頁設(shè)計、網(wǎng)站建設(shè)、微信開發(fā)、小程序制作、集團(tuán)企業(yè)網(wǎng)站建設(shè)等服務(wù)項目。為回饋新老客戶創(chuàng)新互聯(lián)還提供了阿克蘇免費(fèi)建站歡迎大家使用!
工程目錄:
工程一共分為4個模塊,基礎(chǔ)組件app、基礎(chǔ)服務(wù)(包涵路由服務(wù))basecommonlibrary模塊、業(yè)務(wù)模塊libraryone、業(yè)務(wù)模塊librarytwo;
在4個模塊的gradle文件當(dāng)中加入如下代碼:
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如何在這幾個模塊中充當(dāng)路由的角色了。
首先是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(); } }
點(diǎn)擊事件分別響應(yīng)到本module(app)中的兩個activity以及l(fā)ibraryone和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); } }
也是測試了兩個跳轉(zhuǎn),分別跳轉(zhuǎn)到主Module中的Activity中和libraryTwo的Activity中,libraryTwo中的布局和代碼跟libraryone中大同小異,這里就不繼續(xù)貼出了,本項目會在附件中提供源碼,歡迎下載。這里還需要貼出封裝在basecommonlibrary模塊中的公共跳轉(zhuǎn)類,所有的module,都是通過該模塊中的路由公共類來實現(xiàn)跳轉(zhuǎn)的,完整代碼如下:
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); } }); } }
該類中主要提供了幾個類的跳轉(zhuǎn)方法,以及跳轉(zhuǎn)失敗后的信息打印方法,接下來就是測試跳轉(zhuǎn)是否能夠正常執(zhí)行了,請大家自行構(gòu)建代碼或者下載本demo源碼查看執(zhí)行效果。
接下來講解攔截器,以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) { } }
關(guān)于android中怎么利用ARouter實現(xiàn)組件化就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
新聞標(biāo)題:android中怎么利用ARouter實現(xiàn)組件化
本文鏈接:http://vcdvsql.cn/article40/iipjeo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供面包屑導(dǎo)航、App開發(fā)、建站公司、網(wǎng)頁設(shè)計公司、網(wǎng)站維護(hù)、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)