Blogger Information
Blog 5
fans 0
comment 0
visits 6571
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Android项目常用之Rxbus的简单实现和使用
啃腿牛的博客
Original
3389 people have browsed it

前言

RxJava目前已经很火了,对于RxJava这里不多做介绍。
RxBus并不是一个库,而是一种模式。相信大多数开发者都使用过EventBus,作为事件总线通信库,如果你的项目已经加入RxJava和EventBus,不妨用RxBus代替EventBus,以减少库的依赖。

    一。添加Rxjava和RxAndroid依赖

        implementation 'io.reactivex:rxandroid:1.2.1'
        implementation 'io.reactivex:rxjava:1.1.6'

    二。新建Java类

private static volatile Rxbus mInstance;
        private final Subject bus;
        public Rxbus(){
                bus=new SerializedSubject<>(PublishSubject.create());
        }
    
        public static Rxbus getInstance(){
                Rxbus rxbus2=mInstance;
                if(mInstance==null){
                    synchronized (Rxbus.class){
                    rxbus2=mInstance;
                    if(mInstance==null){
                        rxbus2=new Rxbus();
                        mInstance=rxbus2;
                    }
                }
            }
        return rxbus2;
    }
    /**
     * send message
     * @param obj
     */
    public void post(Object obj){
        bus.onNext(obj);
    }
    /**
     *  receive message
     * @param eventType
     * @param <T>
     * @return
     */
    public <T> Observable<T> toObserverable(Class<T> eventType){
        return bus.ofType(eventType);
    }

    tip:

      1、Subject同时充当了Observer和Observable的角色,Subject是非线程安全的,要避免该问题,需要将 Subject转        换 为 一个 SerializedSubject,上述RxBus类中把线程非安全的PublishSubject包装成线程安全的Subject。

      2、PublishSubject只会把在订阅发生的时间点之后来自原始Observable的数据发射给观察者。

      3、ofType操作符只发射指定类型的数据,其内部就是filter+cast

三。创建需要发送的事件类

    public class StudentEvent { 
    private String id;    
    private String name;    
    public StudentEvent(String id, String name) {        
        this.id = id;       
         this.name = name;
    
    }   
     public String getId() { 
          return id;
    
        }    
    public void setId(String id) {
            this.id = id;     
}   
     public String getName() {  
      return name;
    
}    
public void setName(String name) { 
       this.name = name;
  
  } 
}

四。发送事件:

Rxbus.getInstance().post(new StudentEvent("007","zndroid"));

五。接收事件:

 subscribe = Rxbus.getInstance().toObserverable(StudentEvent.class).subscribe(new Action1<StudentEvent>() {
        @Override
        public void call(StudentEvent studentEvent) {
            tv.setText("id:" + studentEvent.getId() + "  name:" + studentEvent.getName());
        }
    });

tip:

        subscription是Sbscription的对象,我们这里把RxBus.getInstance().toObserverable(StudentEvent.class)赋值给       sbscription以方便生命周期结束时取消订阅事件

六。取消订阅:

@Override
    protected void onDestroy() {
        if (!subscribe.isUnsubscribed()){
        subscribe.unsubscribe();
        }
        super.onDestroy();
    }

七。效果图:

dad.gif

参考:

http://wuxiaolong.me/2016/04/07/rxbus/
http://www.jianshu.com/p/ca090f6e2fe2

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!