首先,在gradle中依赖
compile 'io.reactivex:rxandroid:1.2.0'
import rx.Observable;
import rx.functions.Func1;
import rx.subjects.PublishSubject;
import rx.subjects.SerializedSubject;
import rx.subjects.Subject;
/**
* Created by xiaguangcheng on 16/5/17.
*/
public class RxBus {
/**
* 参考网址: http://hanhailong.com/2015/10/09/RxBus%E2%80%94%E9%80%9A%E8%BF%87RxJava%E6%9D%A5%E6%9B%BF%E6%8D%A2EventBus/
* http://www.loongwind.com/archives/264.html
* https://theseyears.gitbooks.io/android-architecture-journey/content/rxbus.html
*/
private static volatile RxBus mDefaultInstance;
private RxBus() {
}
public static RxBus getDefault() {
if (mDefaultInstance == null) {
synchronized (RxBus.class) {
if (mDefaultInstance == null) {
mDefaultInstance = new RxBus();
}
}
}
return mDefaultInstance;
}
private final Subject<Object, Object> _bus = new SerializedSubject<>(PublishSubject.create());
public void send(Object o) {
_bus.onNext(o);
}
public Observable<Object> toObservable() {
return _bus;
}
/**
* 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者
* @param eventType 事件类型
* @param <T>
* @return
*/
public <T> Observable<T> toObservable(Class<T> eventType) {
return _bus.ofType(eventType);
}
/**
* 提供了一个新的事件,根据code进行分发
* @param code 事件code
* @param o
*/
public void post(int code, Object o){
_bus.onNext(new RxBusBaseMessage(code,o));
}
/**
* 根据传递的code和 eventType 类型返回特定类型(eventType)的 被观察者
* 对于注册了code为0,class为voidMessage的观察者,那么就接收不到code为0之外的voidMessage。
* @param code 事件code
* @param eventType 事件类型
* @param <T>
* @return
*/
public <T> Observable<T> toObservable(final int code, final Class<T> eventType) {
return _bus.ofType(RxBusBaseMessage.class)
.filter(new Func1<RxBusBaseMessage,Boolean>() {
@Override
public Boolean call(RxBusBaseMessage o) {
//过滤code和eventType都相同的事件
return o.getCode() == code && eventType.isInstance(o.getObject());
}
}).map(new Func1<RxBusBaseMessage,Object>() {
@Override
public Object call(RxBusBaseMessage o) {
return o.getObject();
}
}).cast(eventType);
}
/**
* 判断是否有订阅者
*/
public boolean hasObservers() {
return _bus.hasObservers();
}
}
public class RxBusBaseMessage {
private int code;
private Object object;
public RxBusBaseMessage(int code, Object object){
this.code=code;
this.object=object;
}
public RxBusBaseMessage(){}
public int getCode() {
return code;
}
public Object getObject() {
return object;
}
}
public class RxCodeConstants {
// 每日推荐跳转对应type下
public static final int JUMP_TYPE = 0;
// 首页跳转到电影栏
public static final int JUMP_TYPE_TO_ONE = 1;
}