加入JakeWharton大神的RxRelay
compile 'com.jakewharton.rxrelay2:rxrelay:2.0.0'
RxBus.java
/**
* If you have any questions, you can contact by email {wangzhumoo@gmail.com}
* @author 王誅魔 2017/11/30 下午6:09
*
*/
public class RxBus {
private Relay<Object> mBus = null;
private static volatile RxBus mDefaultInstance;
public RxBus() {
mBus = PublishRelay.create().toSerialized();
}
public static RxBus getDefault() {
if (mDefaultInstance == null) {
synchronized (RxBus.class) {
if (mDefaultInstance == null) {
mDefaultInstance = new RxBus();
}
}
}
return mDefaultInstance;
}
/**
* 發(fā)送事件
*/
public void post(Object event) {
mBus.accept(event);
}
/**
* 根據(jù)傳遞的 eventType 類型返回特定類型(eventType)的 被觀察者
*/
public <T> Observable<T> toObservable(Class<T> eventType) {
return mBus.ofType(eventType);
}
/**
* 判斷是否有訂閱者
*/
public boolean hasObservers() {
return mBus.hasObservers();
}
/**
* 取消訂閱
* @param disposable
*/
public void unregister(Disposable disposable) {
if (disposable != null && !disposable.isDisposed()) {
disposable.dispose();
}
}
public <T> Disposable register(Class<T> eventType, Scheduler scheduler, Consumer<T> onNext) {
return toObservable(eventType).observeOn(scheduler).subscribe(onNext);
}
public <T> Disposable register(Class<T> eventType, Scheduler scheduler, Consumer<T> onNext, Consumer onError,
Action onComplete, Consumer onSubscribe) {
return toObservable(eventType).observeOn(scheduler).subscribe(onNext, onError, onComplete, onSubscribe);
}
public <T> Disposable register(Class<T> eventType, Scheduler scheduler, Consumer<T> onNext, Consumer onError,
Action onComplete) {
return toObservable(eventType).observeOn(scheduler).subscribe(onNext, onError, onComplete);
}
public <T> Disposable register(Class<T> eventType, Scheduler scheduler, Consumer<T> onNext, Consumer onError) {
return toObservable(eventType).observeOn(scheduler).subscribe(onNext, onError);
}
public <T> Disposable register(Class<T> eventType, Consumer<T> onNext) {
return toObservable(eventType).observeOn(AndroidSchedulers.mainThread()).subscribe(onNext);
}
public <T> Disposable register(Class<T> eventType, Consumer<T> onNext, Consumer onError, Action onComplete, Consumer onSubscribe) {
return toObservable(eventType).observeOn(AndroidSchedulers.mainThread()).subscribe(onNext, onError, onComplete, onSubscribe);
}
public <T> Disposable register(Class<T> eventType, Consumer<T> onNext, Consumer onError, Action onComplete) {
return toObservable(eventType).observeOn(AndroidSchedulers.mainThread()).subscribe(onNext, onError, onComplete);
}
public <T> Disposable register(Class<T> eventType, Consumer<T> onNext, Consumer onError) {
return toObservable(eventType).observeOn(AndroidSchedulers.mainThread()).subscribe(onNext, onError);
}
}
還有一個通用的Event.java
依據(jù)自己的需要制定即可
/**
* If you have any questions, you can contact by email {wangzhumoo@gmail.com}
* @author 王誅魔 2017/10/19 下午5:38
*
* 發(fā)送事件包裝類
*/
public class Event<T> {
public static final int EVENT_CLOSE_ALL_ACTIVITY = 10001;
/**
* reserved data
*/
private T data;
/**
* this code distinguish between different events
*/
private int eventCode = -1;
public Event(int eventCode) {
this(eventCode, null);
}
public Event(int eventCode, T data) {
this.eventCode = eventCode;
this.data = data;
}
/**
* get event code
*
* @return
*/
public int getCode() {
return this.eventCode;
}
/**
* get event reserved data
*
* @return
*/
public T getData() {
return this.data;
}
}
RxBus的使用
注冊
最好是再基類中,加上.
if (useEventBus()) {
//注冊eventbus
Disposable disposable = RxBus.getDefault()
.register(Event.class, new Consumer<Event>() {
@Override
public void accept(Event event) {
int eventCode = event.getCode();
Log.e("RxBus", event.toString());
switch (eventCode) {
case Event.EVENT_CLOSE_ALL_ACTIVITY:
break;
default:
onEvent(event);
break;
}
}
});
addDispose(disposable);
}
其中:
useEventBus()
由子類實現(xiàn),是否開啟RxBus,避免浪費addDispose(disposable);
addDispose 是基類的方法
/**
* RxJava 添加訂閱
*/
protected void addDispose(Disposable disposable) {
if (mDisposables == null) {
mDisposables = new CompositeDisposable();
}
//將所有disposable放入,集中處理
mDisposables.add(disposable);
}
統(tǒng)一管理,銷毀時一起銷毀,避免爆內(nèi)存
- onEvent(event);
是一個空方法,如果子類重寫就把事件交給子類處理
發(fā)送
RxBus.getDefault().post(new Event(100,"message"))
//code最好在Event.java中統(tǒng)一定義,方便管理
處理
override fun onEvent(event: Event<*>?) {
super.onEvent(event)
}
這一部分,其實在 注冊 中已經(jīng)完成,只要子類自己重寫方法,就能取到數(shù)據(jù)