亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Android模塊化中數(shù)據(jù)傳遞/路由跳轉(zhuǎn)實(shí)現(xiàn)示例

 更新時(shí)間:2018年07月07日 09:33:57   作者:左手木亽  
這篇文章主要介紹了Android模塊化中數(shù)據(jù)傳遞/路由跳轉(zhuǎn)實(shí)現(xiàn)示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

雖然說(shuō)模塊通信、路由協(xié)議在Android已經(jīng)不新鮮了,但是如果脫離了那些優(yōu)秀的開(kāi)源庫(kù)我們從零開(kāi)始自己造一個(gè)庫(kù),有時(shí)候重復(fù)造輪子會(huì)讓自己對(duì)所謂"車(chē)"的原理懂得更透徹。

直接上造完的輪子地址: https://github.com/Neacy/NeacyComponent

這個(gè)輪子有兩個(gè)大功能分別是模塊通信以及路由跳轉(zhuǎn):

模塊通信

首先,統(tǒng)一聲明姿勢(shì):

public interface IComponent {

  String getName();

  void startComponent(ComponentParam param);
}

也就是說(shuō),在各自的維護(hù)的模塊內(nèi)若想提供一個(gè)類供別的模塊調(diào)用那么需要實(shí)現(xiàn)這個(gè) IComponent 類,這樣子可以根據(jù)面向?qū)ο蟮膬?yōu)勢(shì)統(tǒng)一管理,所以我們就有了接下來(lái)的這么一個(gè)個(gè)Component類,比如:

@NeacyComponent("app")
public class AppComponent implements IComponent {

  @Override
  public String getName() {
    return "app";
  }

  @Override
  public void startComponent(ComponentParam param) {
    Log.w("Jayuchou", "==== Start AppComponent ====");
    if (param != null && param.getParam().containsKey("callback")) {
      ICallBack callBack = (ICallBack) param.getParam().get("callback");
      Map<String, Object> results = new HashMap<>();
      results.put("result", "我來(lái)自AppComponent");
      ComponentParam cp = new ComponentParam(results);
      callBack.onComponentBack(cp);
    }
  }
}

兩個(gè)地方比較重要:

  1. NeacyComponent 這個(gè)注釋,主要是為后面的gradle掃描使用
  2. getName() 這個(gè)方法返回每個(gè) IComponent 對(duì)應(yīng)的實(shí)例key值,方便在不同的模塊我們可以根據(jù)這個(gè)key值找到對(duì)應(yīng)的 IComponent 對(duì)象

其次,如何調(diào)用呢?

ComponentController.getComponentByName("app").startComponent(null);

是的,只要根據(jù)app這個(gè)key值我們就能輕易的找到對(duì)應(yīng)的 IComponent 對(duì)象,從而執(zhí)行 startComponent ,這個(gè)方法就是你想要在該模塊做的邏輯地方。

看上面我們聲明的 AppComponent 類,我們?cè)?startComponent 有判斷一下傳入的參數(shù)是否為空,這里直接放了一個(gè)偽 Map 類專門(mén)用于存放傳遞的參數(shù)。

如何回調(diào)結(jié)果以及如何獲取別的模塊的回調(diào)結(jié)果?

首先你執(zhí)行了別的模塊的 startComponent 方法,在這個(gè)方法中你返回的類肯定只有對(duì)應(yīng)的模塊能識(shí)別,也就是說(shuō)你在自己模塊獲取不到別的模塊中的類,所以這里使用 ComponentParam 采用key/value的風(fēng)格存放參數(shù)以及回調(diào)返回結(jié)果,然后看一下下面的代碼就能明白答案了。

// 傳遞參數(shù)給IComponent, 可以通過(guò)傳遞回調(diào)函數(shù)從而得到回調(diào)結(jié)果
 Map<String, Object> p = new HashMap<>();
 p.put("callback", new ICallBack() {
   @Override
   public void onComponentBack(ComponentParam result) {
     Log.w("Jayuchou", "==== 運(yùn)行結(jié)果 = " + result.getParam().get("result"));
   }
 });
 ComponentParam cp = new ComponentParam(p);

// 回調(diào)結(jié)果回去
ICallBack callBack = (ICallBack) param.getParam().get("callback");
Map<String, Object> results = new HashMap<>();
results.put("result", "我來(lái)自AppComponent");
ComponentParam cp = new ComponentParam(results);
callBack.onComponentBack(cp);

// 調(diào)用的時(shí)候傳入?yún)?shù)即可
ComponentController.getComponentByName("app").startComponent(cp);

路由跳轉(zhuǎn)

首先,老規(guī)矩肯定也是聲明一下路由協(xié)議(這里只是一個(gè)簡(jiǎn)單的字符串)

@NeacyProtocol("/activity/a")
public class AActivity extends AppCompatActivity

@NeacyProtocol("/activity/b")
public class BActivity extends AppCompatActivity

@NeacyProtocol("/activity/app")
public class MainActivity extends AppCompatActivity

然后調(diào)用就是了:

RouterController.startRouter(MainActivity.this, "/activity/a");// 跳轉(zhuǎn)到AActivity

Bundle args = new Bundle(); 
args.putString("key", "AActivity"); 
RouterController.startRouter(AActivity.this, "/activity/b", args);// 跳轉(zhuǎn)到BActivity并攜帶bundle參數(shù)

原理

原理就是通過(guò)gradle插件結(jié)合ASM掃描注解并在編譯的時(shí)候注入代碼,我們先看下注入成功后的代碼結(jié)構(gòu):

1.模塊通信的注入結(jié)果

public class ComponentController
{
 static
 {
  registerComponent(new AComponent());
  registerComponent(new BComponent());
  registerComponent(new AppComponent());
 }
 
 private static Map<String, IComponent> components = new HashMap();
 
 static void registerComponent(IComponent component)
 {
  components.put(component.getName(), component);
 }
 . 
 . 
 .
}

2.路由跳轉(zhuǎn)注入結(jié)果

public class RouterController
{
 static
 {
  addRouter("/activity/a", "com.neacy.neacy_a.AActivity");
  addRouter("/activity/b", "com.neacy.neacy_b.BActivity");
  addRouter("/activity/app", "com.neacy.component.MainActivity");
 }
 
 private static Map<String, String> routers = new HashMap();
 
 public static void addRouter(String key, String value)
 {
  routers.put(key, value);
 }
}

3.更多gradle插件的代碼查閱 https://github.com/Neacy/NeacyComponent

最后

再次感謝靈感: https://github.com/luckybilly/CC

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android自定義View仿華為圓形加載進(jìn)度條

    Android自定義View仿華為圓形加載進(jìn)度條

    這篇文章主要為大家詳細(xì)介紹了Android自定義View仿華為圓形加載進(jìn)度條,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android MediaPlayer實(shí)現(xiàn)音樂(lè)播放器實(shí)例代碼

    Android MediaPlayer實(shí)現(xiàn)音樂(lè)播放器實(shí)例代碼

    這篇文章主要介紹了Android MediaPlayer實(shí)現(xiàn)音樂(lè)播放器實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-01-01
  • Android實(shí)現(xiàn)布局全屏

    Android實(shí)現(xiàn)布局全屏

    這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)布局全屏,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android實(shí)現(xiàn)通知欄透明的方法

    Android實(shí)現(xiàn)通知欄透明的方法

    這個(gè)特性是andorid4.4支持的,最少要api19才可以使用,也就是說(shuō)如果Android的機(jī)子是低于4.4,沉浸通知欄是沒(méi)有效果的。下面介紹一下使用的方法,非常得簡(jiǎn)單,對(duì)android通知欄透明相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧
    2016-01-01
  • Android控件ListView使用方法詳解

    Android控件ListView使用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Android控件ListView的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-07-07
  • WebView設(shè)置WebViewClient的方法

    WebView設(shè)置WebViewClient的方法

    這篇文章主要介紹了 WebView設(shè)置WebViewClient的方法的相關(guān)資料,希望通過(guò)本文能幫助到大家,需要的朋友可以參考下
    2017-09-09
  • android實(shí)現(xiàn)listview分頁(yè)的方法

    android實(shí)現(xiàn)listview分頁(yè)的方法

    這篇文章主要介紹了android實(shí)現(xiàn)listview分頁(yè)的方法,涉及Android生成listview列表的相關(guān)技巧,需要的朋友可以參考下
    2015-05-05
  • ViewDragHelper實(shí)現(xiàn)QQ側(cè)滑效果

    ViewDragHelper實(shí)現(xiàn)QQ側(cè)滑效果

    這篇文章主要為大家詳細(xì)介紹了ViewDragHelper實(shí)現(xiàn)QQ側(cè)滑效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Android列表實(shí)現(xiàn)單選點(diǎn)擊縮放動(dòng)畫(huà)效果

    Android列表實(shí)現(xiàn)單選點(diǎn)擊縮放動(dòng)畫(huà)效果

    在android開(kāi)發(fā),我們會(huì)常常使用到縮放動(dòng)畫(huà),這篇文章主要給大家介紹了關(guān)于Android列表實(shí)現(xiàn)單選點(diǎn)擊縮放動(dòng)畫(huà)效果的相關(guān)資料,需要的朋友可以參考下
    2021-08-08
  • Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼

    Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼

    利用Android的ViewFlipper和AnimationUtils實(shí)現(xiàn)圖片帶有動(dòng)畫(huà)的輪播切換,其中當(dāng)點(diǎn)擊“上一張”圖片時(shí),切換到上一張圖片;當(dāng)點(diǎn)擊“下一張”圖片時(shí),切換到下一張圖片,本文給大家介紹Android實(shí)現(xiàn)圖片輪播切換實(shí)例代碼,需要的朋友參考下
    2015-12-12

最新評(píng)論