天天看點

Android 事件總線Otto架構詳解二

如果你是剛學習這個,或者是對這個架構還不夠了解它的工作機理,那麼你可以在你去看看我的另一篇文章。

http://blog.csdn.net/songkai320/article/details/51786600 

本篇文章我們接着上一篇的來講,關于這個配置其實很簡單,關鍵是怎麼使用這個架構來寫出更好的代碼。在這裡我們在上次工程的基礎上建立一個抽象的事件類,這樣友善程式的擴充。

import android.os.Bundle;

public abstract class RootEvent {
    // Data
    public Bundle data;
}      

然後我們再建立兩個事件類來繼承上述抽象事件類:

import android.os.Bundle;

public class LeafEventA extends RootEvent {
    public enum EventA {
        EventA_1,
        EventA_2,
        EventA_3
    }
    public EventA what;
    public LeafEventA(EventA what) {
        this.what = what;
    }
    public LeafEventA(EventA what, Bundle data) {
        this.what = what;
        this.data = data;
    }
}      
public class LeafEventB extends RootEvent {
    public enum EventB {
        EventB_1,
        EventB_2
    }
    public EventB what;
    public LeafEventB(EventB what) {
        this.what = what;
    }
    public LeafEventB(EventB what, Bundle data) {
        this.what = what;
        this.data = data;
    }
}
      

寫好了事件,接下來我們要在MainActivity中通過點選按鈕來釋出,然後為了友善我們還是把訂閱者方法同樣寫在MainActivity中:

public void postEventA_1(View v) {
    Bundle data=new Bundle();
    data.putString("EventA_1_Data","沒錯,我就是EventA_1的資料!");
    App.bus.post(new LeafEventA(LeafEventA.EventA.EventA_1,data));
}
public void postEventA_2(View v) {
    Bundle data=new Bundle();
    data.putString("EventA_2_Data","沒錯,我就是EventA_2的資料!");
    App.bus.post(new LeafEventA(LeafEventA.EventA.EventA_2,data));
}
public void postEventA_3(View v) {
    Bundle data=new Bundle();
    data.putString("EventA_3_Data","沒錯,我就是EventA_3的資料!");
    App.bus.post(new LeafEventA(LeafEventA.EventA.EventA_3,data));
}
public void postEventB_1(View v) {
    Bundle data=new Bundle();
    data.putString("EventB_1_Data","沒錯,我就是EventB_1的資料!");
    App.bus.post(new LeafEventB(LeafEventB.EventB.EventB_1,data));
}
public void postEventB_2(View v) {
    Bundle data=new Bundle();
    data.putString("EventB_2_Data","沒錯,我就是EventB_2的資料!");
    App.bus.post(new LeafEventB(LeafEventB.EventB.EventB_2,data));
}      

上面都是各個按鈕的點選事件,我是在布局檔案中寫下的onClick屬性。

下面是A訂閱者方法:

@Subscribe
public void onEvent(LeafEventA leafEventA){
    switch(leafEventA.what){
        case EventA_1:
            Log.i(TAG,"This is EventA_1"+"收到的資料是:"+leafEventA.data.getString("EventA_1_Data"));
            break;
        case EventA_2:
            Log.i(TAG,"This is EventA_2"+"收到的資料是:"+leafEventA.data.getString("EventA_2_Data"));
            break;
        case EventA_3:
            Log.i(TAG,"This is EventA_3"+"收到的資料是:"+leafEventA.data.getString("EventA_3_Data"));
            break;
        default:
    }
}      

下面是B訂閱者方法:

@Subscribe
public void onEvent(LeafEventB leafEventB){
    switch(leafEventB.what){
        case EventB_1:
            Log.i(TAG,"This is EventB_1"+"收到的資料是:"+leafEventB.data.getString("EventB_1_Data"));
            break;
        case EventB_2:
            Log.i(TAG,"This is EventB_2"+"收到的資料是:"+leafEventB.data.getString("EventB_2_Data"));
            break;
        default:
    }
}      

OK ,接下來Ctrl+F9編譯一下,然後運作,點選各個按鈕檢視列印資訊:

06-30 13:23:14.454 12057-12057/com.myapplication I/MainActivity: This is EventA_1收到的資料是沒錯,我就是EventA_1的資料!

06-30 13:23:23.782 12057-12057/com.myapplication I/MainActivity: This is EventA_2收到的資料是沒錯,我就是EventA_2的資料!

06-30 13:23:25.913 12057-12057/com.myapplication I/MainActivity: This is EventA_3收到的資料是沒錯,我就是EventA_3的資料!

06-30 13:23:27.803 12057-12057/com.myapplication I/MainActivity: This is EventB_1收到的資料是沒錯,我就是EventB_1的資料!

06-30 13:23:29.547 12057-12057/com.myapplication I/MainActivity: This is EventB_2收到的資料是沒錯,我就是EventB_2的資料!

怎麼樣,是不是很給力。如果你說不是,我也同意,畢竟我也不是師者,想要傳道授業解惑,怎奈何學識低淺,我們一起努力吧。