EventBus封装

EventBus封装使用

封装

1
implementation 'org.greenrobot:eventbus:3.2.0'
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
import org.greenrobot.eventbus.EventBus;
import xxx.entity.Event;

/**
* EventBus的工具类
*/
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(Event event) {
EventBus.getDefault().post(event);
}

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

// 其他
}
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 Event<T> {
private int code;
private T data;

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

public Event(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;
}
}

BaseActivity/BaseFragment

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import android.os.Bundle;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import xxx.entity.Event;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public class BaseActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (isRegisterEventBus()){
EventBus.getDefault().register(this);
}
}

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



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

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

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

/**
* 接收到分发到事件
* 子类需要EventBus的话需要重写这个类
*
* @param event 事件
*/
protected void receiveEvent(Event event) {

}

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

}
//*******************************EventBus************************************


}

使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
//可选
/**
* 定义Event的code
*/
public final class C {
// EventBus Code
public static final class EventCode {
public static final int A = 0x111111;
public static final int B = 0x222222;
public static final int C = 0x333333;
public static final int D = 0x444444;
// other more
}
}
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
import android.os.Bundle;

import androidx.annotation.Nullable;

import xxx.entity.Event;
import xxx.entity.User;

public class MainActivity extends BaseActivity{
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xxx);

//在需要的地方发送事件(别的类中)
//其中的code和data替换成自己的
/*EventBusUtil.sendEvent(new Event(C.EventCode.A));

EventBusUtil.sendEvent(new Event(C.EventCode.B));

Event<User> event = new Event<>(C.EventCode.C, new User());
EventBusUtil.sendEvent(event);

Event<News> event = new Event<>(C.EventCode.D, new News());
EventBusUtil.sendEvent(event);*/
}

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

@Override
protected void receiveEvent(Event event) {
// 接受到Event后的相关逻辑
switch (event.getCode()) {
case C.EventCode.A:
Log.d("EventBus", "接收到A类型的Event");
break;
case C.EventCode.B:
Log.d("EventBus", "接收到B类型的Event");
break;
case C.EventCode.C:
Log.d("EventBus", "接收到B类型的Event,携带User");
User user = (User) event.getData();
break;
case C.EventCode.D:
Log.d("EventBus", "接收到D类型的Event,携带News");
News news = (News) event.getData();
break;
}
}
}