如果你是刚学习这个,或者是对这个框架还不够了解它的工作机理,那么你可以在你去看看我的另一篇文章。
本篇文章我们接着上一篇的来讲,关于这个配置其实很简单,关键是怎么使用这个框架来写出更好的代码。在这里我们在上次工程的基础上新建一个抽象的事件类,这样方便程序的扩展。
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的数据!
怎么样,是不是很给力。如果你说不是,我也同意,毕竟我也不是师者,想要传道授业解惑,怎奈何学识低浅,我们一起努力吧。