当前位置: 代码迷 >> 综合 >> RxBus 简单使用学习
  详细解决方案

RxBus 简单使用学习

热度:1   发布时间:2024-01-18 10:52:31.0

最近需要重构项目,准备用EventBus,后来想了想还是用新的技术比较好点,所以用了RxBus ,但是我这里用的是RxBus2,RxBus2的用法跟RxBus的用法不一样,改了很多地方,大家可以在网上搜索下,我这里只是提供最简单的应用场景用法。

1.我们知道EventBus,RxBus2,的目的就是为了组件之间的通讯。
应用场景:在购物车的Fragment中 正价商品数量,然后让主Activity 底部购物车数量提示数量实时更新,如何做到呢,我们就用这个RxBus2。

导入:

    //rxjava2 startcompile 'io.reactivex.rxjava2:rxjava:2.0.3'compile 'io.reactivex.rxjava2:rxandroid:2.0.1'//rxjava2 end
package com.dk.basepack.bseaapplication.util;import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.subjects.PublishSubject;
import io.reactivex.subjects.Subject;/*** Created by Administrator on 2017/10/10.*/public class RxBus {
    private volatile static RxBus mDefaultInstance;private final Subject<Object> mBus;private final Map<Class<?>, Object> mStickyEventMap;public RxBus() {mBus = PublishSubject.create().toSerialized();mStickyEventMap = new ConcurrentHashMap<>();}public static RxBus getInstance() {if (mDefaultInstance == null) {synchronized (RxBus.class) {if (mDefaultInstance == null) {mDefaultInstance = new RxBus();}}}return mDefaultInstance;}/*** 发送事件*/public void post(Object event) {mBus.onNext(event);}/*** 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者*/public <T> Observable<T> toObservable(final Class<T> eventType) {return mBus.ofType(eventType);}/*** 判断是否有订阅者*/public boolean hasObservers() {return mBus.hasObservers();}public void reset() {mDefaultInstance = null;}/*** Stciky 相关*//*** 发送一个新Sticky事件*/public void postSticky(Object event) {synchronized (mStickyEventMap) {mStickyEventMap.put(event.getClass(), event);}post(event);}/*** 根据传递的 eventType 类型返回特定类型(eventType)的 被观察者*/public <T> Observable<T> toObservableSticky(final Class<T> eventType) {synchronized (mStickyEventMap) {Observable<T> observable = mBus.ofType(eventType);final Object event = mStickyEventMap.get(eventType);if (event != null) {return observable.mergeWith(Observable.create(new ObservableOnSubscribe<T>() {@Overridepublic void subscribe(ObservableEmitter<T> subscriber) throws Exception {subscriber.onNext(eventType.cast(event));}}));} else {return observable;}}}/*** 根据eventType获取Sticky事件*/public <T> T getStickyEvent(Class<T> eventType) {synchronized (mStickyEventMap) {return eventType.cast(mStickyEventMap.get(eventType));}}/*** 移除指定eventType的Sticky事件*/public <T> T removeStickyEvent(Class<T> eventType) {synchronized (mStickyEventMap) {return eventType.cast(mStickyEventMap.remove(eventType));}}/*** 移除所有的Sticky事件*/public void removeAllStickyEvents() {synchronized (mStickyEventMap) {mStickyEventMap.clear();}}/*** 普通订阅解绑* @param disposable*/public static   void  rxBusUnbund(CompositeDisposable  disposable){if (null != disposable && !disposable.isDisposed()) {disposable.clear();}}
}

使用:

发送:

    RxBus.getInstance().post(new SetShoppingGoodsCount(Run.goodsCounts));

接收:

compositeDisposable = new CompositeDisposable();RxBus.getInstance().toObservable(SetShoppingGoodsCount.class).subscribe(new Observer<SetShoppingGoodsCount>() {@Overridepublic void onSubscribe(Disposable d) {compositeDisposable.add(d);}@Overridepublic void onNext(SetShoppingGoodsCount setShoppingGoodsCount) {setShoppingCarCountWithoutAnimation(setShoppingGoodsCount.getCount());}@Overridepublic void onError(Throwable e) {}@Overridepublic void onComplete() {}});

解绑,如果不解绑就会造成内存溢出

@Overrideprotected void onDestroy() {super.onDestroy();RxBus.rxBusUnbund(compositeDisposable);}

Sticky,上面已经有了封装,如果想用,看看推荐的链接。这算是RxBus最简单的使用了,更复杂的我也在研究,学习一下。


推荐看:
[深入RxBus]:支持Sticky事件
http://www.jianshu.com/p/71ab00a2677b

http://www.jianshu.com/p/f43903b28cc2