rxAndroid之RxBus

RxBus开源地址

RxBus介绍

实现页面间/页面内传值,可以使用tag对消息进行标记,可以指定线程。

RxBus简单使用
package com.example.rxtest;

import rx.Observable;
import rx.subjects.PublishSubject;

/**
 * Created by Administrator on 2017/8/4 0004.
 */

public class RxBus {

    private final static String TAG = "RxBus";

    private volatile static RxBus mInstance;

    /**
     * 继承Observable 用于给RXBus发送消息
     */
    private PublishSubject mEventBus;

    private RxBus(){

        mEventBus = PublishSubject.create();

    }

    public static RxBus getInstance(){

        if(mInstance == null){
            synchronized (RxBus.class){
                if(mInstance == null){
                    mInstance = new RxBus();
                }
            }
        }
        return mInstance;
    }

    /**
     * 发送消息
     */
    public void post(Object event){
        mEventBus.onNext(event);
    }

    /**
     * 获取mEventBus
     */
    public Observable toObservable(){
        return mEventBus;
    }
}
 
 
简单介绍

创建被观察者,相当于数据总线,提供数据源,发送消息

private PublishSubject mEventBus;
 
 

发送消息

/**
     * 发送消息
     */
    public void post(Object event){
        mEventBus.onNext(event);
    }

获取数据总线

 /**
     * 获取mEventBus
     */
    public Observable toObservable(){
        return mEventBus;
    }
 
 

简单代码示例

MainActivity

package com.example.rxtest;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.subscriptions.CompositeSubscription;


public class MainActivity extends AppCompatActivity {


    private static final String TAG = "MainActivity";

    private static CompositeSubscription mSubscription;

    @BindView(R.id.btn_search)
    Button btnSearch;
    @BindView(R.id.tv_result)
    TextView tvResult;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        addSubscription(subscribeEvent());

        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                startActivity(new Intent(MainActivity.this,TestActivity.class));

            }
        });

    }

    @Override
    protected void onDestroy() {
        if(mSubscription != null){
            mSubscription.clear();
        }
        super.onDestroy();
    }

    private void addSubscription(Subscription subscription){

        if(subscription == null) return;
        if(mSubscription == null){
            mSubscription = new CompositeSubscription();
        }

        mSubscription.add(subscription);
    }
//订阅事件
    private Subscription subscribeEvent(){
        return RxBus.getInstance().toObservable()
                .observeOn(AndroidSchedulers.mainThread())
                .doOnNext(new Action1() {
                    @Override
                    public void call(Object o) {
                        if(o instanceof MyEvent){
                            doChange((MyEvent)o);
                        }
                    }
                })
                .subscribe();

    }

    private void doChange(MyEvent event) {

        tvResult.setText(event.content);

    }
}
 
 

TestActivity

public class TestActivity extends AppCompatActivity {

    @BindView(R.id.btn_change)
    Button btnSearch;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_test);
        ButterKnife.bind(this);

    }

    @OnClick(R.id.btn_change)
    public void BtnChange(View view){

        String des = "你好啊";
        //发送消息
        RxBus.getInstance().post(new MyEvent(des));

    }
}

MyEvent

package com.example.rxtest;

/**
 * Created by Administrator on 2017/8/4 0004.
 */

public class MyEvent {

    public String content;

    public MyEvent(String content){
        this.content = content;
    }
}

RxBus

package com.example.rxtest;

import rx.Observable;
import rx.subjects.PublishSubject;

/**
 * Created by Administrator on 2017/8/4 0004.
 */

public class RxBus {

    private final static String TAG = "RxBus";

    private volatile static RxBus mInstance;

    /**
     * 继承Observable 用于给RXBus发送消息
     */
    private PublishSubject mEventBus;

    private RxBus(){

        mEventBus = PublishSubject.create();

    }

    public static RxBus getInstance(){

        if(mInstance == null){
            synchronized (RxBus.class){
                if(mInstance == null){
                    mInstance = new RxBus();
                }
            }
        }
        return mInstance;
    }

    /**
     * 发送消息
     */
    public void post(Object event){
        mEventBus.onNext(event);
    }

    /**
     * 获取mEventBus
     */
    public Observable toObservable(){
        return mEventBus;
    }
}
 
 
点击按钮前MainActivity
TestActivity
点击后的MainAcitivity

你可能感兴趣的:(rxAndroid之RxBus)