1. 程式人生 > >Rxjava實現eventbus效果

Rxjava實現eventbus效果

//匯入依賴

compile 'io.reactivex:rxandroid:1.1.0'
compile 'io.reactivex:rxjava:1.1.2'
compile'com.trello:rxlifecycle:0.3.0'
compile'com.trello:rxlifecycle-components:0.3.0'
//RXBus工具類
public class RxBusUtils {
    private static volatile RxBusUtils instance;
    private SerializedSubject<Object, Object> bus;
    public RxBusUtils(){
        bus = new SerializedSubject<>(PublishSubject.create());
    }
    public static RxBusUtils getInstance(){
            if(instance==null){
                synchronized (RxBusUtils.class){
                    if(instance==null){
                        instance= new RxBusUtils();
                    }
                }
            }
            return instance;
        }
    public void post(Object o){
        bus.onNext(o);
    }
    public <T> Observable<T> tObservable(Class<T> event) {
        return bus.ofType(event);
    }
}

//activity
public class MainActivity extends RxAppCompatActivity implements View.OnClickListener {

    private Button btn;
    private TextView textView;
    private Subscription subscribe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button) findViewById(R.id.btn);
        textView = (TextView) findViewById(R.id.tv);
        btn.setOnClickListener(this);
        //      //在沒有忘記寫防止記憶體洩漏使用接收方法 MainActivity extends AppCompatActivity
//        subscribe = RxBusUtils.getInstance().tObservable(Person.class)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .subscribe(new Action1<Person>() {
//                    @Override
//                    public void call(Person person) {
//                        textView.setText(person.getName());
//                    }
//                });
        //在忘記寫防止記憶體洩漏使用接收方法 MainActivity extends RxAppCompatActivity
        Observable<Person> observable = RxBusUtils.getInstance().tObservable(Person.class);
        Subscription subscribe = observable
                .compose(this.bindUntilEvent(ActivityEvent.DESTROY))
                .subscribe();
        subscribe = observable
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Person>() {
                    @Override
                    public void call(Person person) {
                        textView.setText(person.getName());
                    }
                });

    }



    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn:
                Person person = new Person("新資料,456");
                RxBusUtils.getInstance().post(person);
                break;
        }
    }

//    @Override
    //     // 防止記憶體洩漏
//    protected void onDestroy() {
//        super.onDestroy();
//        if (subscribe!=null) {
//            if(subscribe.isUnsubscribed()){
//                subscribe.unsubscribe();
//            }
//        }
//    }
}