- Notifications
You must be signed in to change notification settings - Fork1
Event Bus By RxJava. 使用RxJava实现的事件发布/订阅框架,替代EventBus,支持Sticky Event,支持生命周期管理。
License
NotificationsYou must be signed in to change notification settings
Z-P-J/RxBus
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
// RxJava2 implementation'io.reactivex.rxjava2:rxjava:2.2.17'// RxAndroid implementation'io.reactivex.rxjava2:rxandroid:2.1.1'// RxBus implementation'com.github.Z-P-J:RxBus:latest_version'
二、使用(查看 demo)
// 订阅自定义的Event类型事件RxBus.observe(object,Event.class) .bindTag("TAG")// 绑定TAG,可根据该TAG移除订阅 .bindView(tvText)// 绑定View,当View销毁时自动取消订阅 .bindToLife(this,Lifecycle.Event.ON_PAUSE)// 绑定Activity/Fragment生命周期 .doOnNext(newConsumer<Event>() {@Overridepublicvoidaccept(Eventevent)throwsException {// 接收Event类型的信息 } }) .doOnError(newConsumer<Throwable>() {@Overridepublicvoidaccept(Throwablethrowable)throwsException {// 订阅出错了 } }) .doOnComplete(newAction() {@Overridepublicvoidrun()throwsException {// 取消订阅 } }) .doOnSubscribe(newConsumer<Disposable>() {@Overridepublicvoidaccept(Disposabledisposable)throwsException {// 开始订阅 } }) .subscribe();// 发送Event类型事件RxBus.post(newEvent());
// 订阅Key事件RxBus.observe(object,"Key") .doOnNext(newConsumer<String>() {@Overridepublicvoidaccept(Strings)throwsException {// 收到信息 } }) .subscribe();// 发送Key事件RxBus.post("Key");
// 订阅者1:订阅Key1和Boolean类型事件RxBus.observe(object,"Key1",Boolean.class) .doOnNext(newSingleConsumer<Boolean>() {@OverridepublicvoidonAccept(Booleanevent)throwsException {// 收到event1 } }) .subscribe();// 订阅者2:订阅Key2和Boolean类型事件RxBus.observe(object,"Key2",Boolean.class) .doOnNext(newSingleConsumer<Boolean>() {@OverridepublicvoidonAccept(Booleanevent)throwsException {// 收到event } }) .subscribe();// 订阅者3:订阅Key1和Integer类型事件RxBus.observe(object,"Key1",Integer.class) .doOnNext(newSingleConsumer<Integer>() {@OverridepublicvoidonAccept(Integerevent)throwsException {// 收到event } }) .subscribe();// 发送Key and Event事件RxBus.post("Key1",true);// 订阅者1将收到事件RxBus.post("Key2",false);// 订阅者2将收到事件RxBus.post("Key1",1);// 订阅者3将收到事件
// 当我们要同时传送两个参数时我们无需自定义新的Event类,可直接使用以下方法RxBus.observe(object,"Key",Boolean.class,Integer.class) .doOnNext(newRxBus.PairConsumer<Boolean,Integer>() {@OverridepublicvoidonAccept(Booleanb,Integeri) {// 收到信息: b=true and i=100 } }) .subscribe();// 发送Key事件RxBus.post("Key",true,100);// 当我们要同时传送三个参数时我们无需自定义新的Event类,可直接使用以下方法RxBus.observe(object,"Key",String.class,Boolean.class,Double.class) .doOnNext(newRxBus.TripleConsumer<String,Boolean,Integer>() {@OverridepublicvoidonAccept(Strings,Booleanb,Doubled) {// 收到信息: s="msg" and b=false and d=100.0 } }) .subscribe();// 发送数据RxBus.post("Key","msg",false,100.0);// 注意:若发送以下数据上面的订阅者将接收不到,第三个参数必须是Double类型才能接收到RxBus.post("Key","msg",false,100);
/* 支持Sticky事件,只需调用RxBus.withSticky和RxBus.postSticky,其他用法同上 */// 订阅Sticky事件RxBus.observeSticky(...)// 发送Sticky事件RxBus.postSticky(...);// 获取Sticky事件RxBus.getStickyEvent("key");RxBus.getStickyEvent(clazz);RxBus.getStickyEvent("key",clazz);// 移除Sticky事件RxBus.removeStickyEvent("key");RxBus.removeStickyEvent(clazz);RxBus.removeStickyEvent("key",clazz);RxBus.removeAllStickyEvents();
6. 生命周期管理(使用RxLife框架实现)
/* 订阅者1、2和3都依赖于object1,订阅者4依赖于object2 */// 订阅者1RxBus.observe(object1,"Key1",Event1.class) .bindTag("TAG1")// 绑定TAG,可根据该TAG移除订阅 .bindView(bindView)// 绑定View,当View销毁时自动取消订阅 .bindToLife(this,Lifecycle.Event.ON_PAUSE)// 绑定Activity/Fragment的onPause生命周期 .bindToLife(this)// 绑定Activity/Fragment生命周期,默认为onDestroy时移除取消订阅 .subscribe();// 订阅者2RxBus.observe(object1,"Key2",Event1.class) .bindTag("TAG2") .subscribe();// 订阅者3RxBus.observe(object1,"Key1",Event2.class) .bindTag("TAG3") .subscribe();// 订阅者4RxBus.observe(object2,"Key1",Event2.class) .bindTag("TAG3") .subscribe();// 移除bindView,当bindView调用onViewDetachedFromWindow时将自动取消订阅者1view.removeView(bindView);// 当Activity/Fragment onPause时将自动取消订阅者1onPause();// 根据依赖的Object取消订阅RxBus.removeObservers(object1);// 订阅者1、订阅者2和订阅者3都会被取消RxBus.removeObservers(object2);// 只取消订阅者4// 根据TAG取消订阅RxBus.removeObservers("TAG1");// 只取消订阅者1RxBus.removeObservers("TAG2");// 只取消订阅者2RxBus.removeObservers("TAG3");// 将取消订阅者3和订阅者4// 根据Key取消订阅RxBus.removeObservers("Key1");// 将取消订阅者1、订阅者3和订阅者4RxBus.removeObservers("Key2");// 只取消订阅者2
Copyright 2019 Z-P-J Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
About
Event Bus By RxJava. 使用RxJava实现的事件发布/订阅框架,替代EventBus,支持Sticky Event,支持生命周期管理。
Topics
Resources
License
Stars
Watchers
Forks
Packages0
No packages published