EventBus 3.0 封装

封装思路

  • 封装一个EventBus工具类,消息类MessageEvent
  • 在BaseActivity中选择注册及接收消息,默认不注册
  • 子Activity继承BaseActivity,如需接收消息,则重写父类的方法,返回true
  • 在需要发送消息的地方使用工具类发送消息

具体步骤

封装EventBusUtil类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class EventBusUtil {
public static void register(Object subscriber) {
EventBus.getDefault().register(subscriber);
}

public static void unregister(Object subscriber) {
EventBus.getDefault().unregister(subscriber);
}

public static void sendEvent(MessageEvent event) {
EventBus.getDefault().post(event);
}

public static void sendStickyEvent(MessageEvent event) {
EventBus.getDefault().postSticky(event);
}
}

封装MessageEvent

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
public class MessageEvent<T> {
private int code;
private T data;

public MessageEvent(int code) {
this.code = code;
}

public MessageEvent(int code, T data) {
this.code = code;
this.data = data;
}

public int getCode() {
return code;
}

public void setCode(int code) {
this.code = code;
}

public T getData() {
return data;
}

public void setData(T data) {
this.data = data;
}
}

其中code对应不同的消息,data为发送的内容

在BaseActivity中选择注册及接收消息,默认不注册

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
public class BaseActivity extends Activity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isRegisterEventBus()) {
EventBusUtil.register(this);
}
}

@Override
protected void onDestroy() {
super.onDestroy();
if (isRegisterEventBus()) {
EventBusUtil.unregister(this);
}
}

/**
* 是否注册事件分发
*
* @return true绑定EventBus事件分发,默认不绑定,子类需要绑定的话复写此方法返回true.
*/
protected boolean isRegisterEventBus() {
return false;
}

@Subscribe(threadMode = ThreadMode.MAIN)
public void onEventBusCome(MessageEvent event) {
if (event != null) {
receiveEvent(event);
}
}

@Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onStickyEventBusCome(MessageEvent event) {
if (event != null) {
receiveStickyEvent(event);
}
}

/**
* 接收到分发到事件
*
* @param event 事件
*/
protected void receiveEvent(MessageEvent event) {

}

/**
* 接受到分发的粘性事件
*
* @param event 粘性事件
*/
protected void receiveStickyEvent(MessageEvent event) {

}
}

子Activity继承BaseActivity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ebfirst);
ButterKnife.bind(this);
}

@OnClick(R.id.button)
public void onViewClicked() {
EventBusUtil.sendStickyEvent(new MessageEvent<>(MyEventCode.CODE_B, mEditText.getText().toString()));
startActivity(new Intent(this, SecondActivity.class));
}

@Override
protected boolean isRegisterEventBus() {
return true;
}

@Override
protected void receiveEvent(MessageEvent event) {
switch (event.getCode()) {
case MyEventCode.CODE_A:
String data = (String) event.getData();
mTextView.setText(data);
break;
}
}

@Override
protected void receiveStickyEvent(MessageEvent event) {
switch (event.getCode()) {

}
}

Powered by AppBlog.CN     浙ICP备14037229号

Copyright © 2012 - 2020 APP开发技术博客 All Rights Reserved.

访客数 : | 访问量 :