調(diào)研了一下目前的路由框架,ARouter(阿里的),ActivityRouter都使用了apt技術(shù) 編譯時(shí)注解,個(gè)人想法是一口吃不成胖子,先做個(gè)比較實(shí)用的。 VpRouter路由框架主要應(yīng)用于組件化開發(fā)中
設(shè)計(jì)目的
- 解耦
- 跨模塊跳轉(zhuǎn)
- 方便服務(wù)器配置schema,實(shí)現(xiàn)動(dòng)態(tài)配置跳轉(zhuǎn)目標(biāo)
- 對(duì)外部提供遠(yuǎn)程訪問的功能,實(shí)現(xiàn)跨應(yīng)用調(diào)用響應(yīng)
主要功能點(diǎn)
- 支持intent,http,schema三種跳轉(zhuǎn)
- 路由表支持xml配置,可自定義,支持多路徑
- 有攔截器
- 同時(shí)支持反射和隱式意圖
- 支持結(jié)果回調(diào)
- 支持參數(shù)傳遞
- 鏈?zhǔn)秸{(diào)用
- 支持url模式傳參
- 支持配置多個(gè)webview 實(shí)現(xiàn)指定非默認(rèn)的webview啟動(dòng)url
- 支持配置多個(gè)prefix
重要的類
- VpRouter 單例模式 入口類
- AbsRouter 路由抽象類 主要代碼
- RouterTable 路由表
- IRouterInterceptor(攔截器) IRouterResultCallback(結(jié)果回調(diào))
類圖

vprouter.png
加載路由配置文件
//導(dǎo)入路由表 在application的onCreate中
VpRouter.load(getApplicationContext(),"router.xml");
路由配置文件
<?xml version="1.0" encoding="utf-8" ?>
<root>
schema-prefix>
<prefix>vf://</prefix>
<prefix>vipjr://</prefix>
<prefix>vpjr://</prefix>
</schema-prefix>
<default-webview>vpjr://h5.page</default-webview>
<rule>
<schema>vpjr://h5.page</schema>
<!--<action>webview</action>-->
<class>com.vip.hybrid.h5container.H5WebViewActivity</class>
</rule>
<rule>
<schema>vpjr://h5.page.pay</schema>
<!--<action>webview</action>-->
<class>com.vip.vpal.paydesk.support.h5.H5ContainerActivity</class>
</rule>
<rule>
<schema>vpjr://paycode.entry</schema>
<!--<action>1111</action>-->
<class>com.vip.vpal.paycode.presentation.activity.PaymentEntryActivity</class>
</rule>
<rule>
<schema>vpjr://guide</schema>
<!--<action>basemodule.test.mainactivity</action>-->
<class>com.vip.vf.android.GuideActivity</class>
</rule>
</root>
- schema-prefix: 前綴
- default-webview: 默認(rèn)webview的schema
- 每一個(gè)rule節(jié)點(diǎn)代表一組路由規(guī)則,被解析成Rule對(duì)象
使用示例
//啟動(dòng)url 默認(rèn)的webview
VpRouter.get().context(this).jump("http://www.vip.com?web_title=唯品會(huì)");
//指定webview啟動(dòng)
VpRouter.get().context(this).webView("vpjr://h5.page.pay").jump("http://www.vip.com");
//最復(fù)雜的使用
VpRouter.get()
.context(this)
.setInterceptor(new IRouterInterceptor() {
@Override
public boolean cancel() {
return false;//return true 會(huì)把本次跳轉(zhuǎn)攔截掉
}
@Override
public Bundle addExtras() {
return null;//返回Bundle 會(huì)添加到Intent中
}
})
.setResultCallback(new IRouterResultCallback() {
@Override
public void onSuccess() {
//跳轉(zhuǎn)成功的回調(diào)
}
@Override
public void onFail(RouterError error) {
//跳轉(zhuǎn)失敗的回調(diào)
LogUtils.d("error:"+error);
}
})
.extra("key","value")
.extra("key2","value2")
.flags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
.jump("vf://paycode.entry?title=1111");//支持url傳參
//最簡(jiǎn)單的使用
VpRouter.get().context(this).jump("vpjr://guide");
VpRouter.get().context(this).jumpForResult("vpjr://guide",REQUEST_CODE);//REQUEST_CODE>0