美文网首页
rxjava和rxandroid简单的使用

rxjava和rxandroid简单的使用

作者: Bruce杨 | 来源:发表于2018-01-22 11:26 被阅读0次
    compile 'io.reactivex.rxjava2:rxjava:2.0.4'
    compile 'io.reactivex.rxjava2:rxandroid:2.0.1'
    
    package com.supermap.rxjavademo;
    
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.TextView;
    
    import io.reactivex.Observable;
    import io.reactivex.ObservableEmitter;
    import io.reactivex.ObservableOnSubscribe;
    import io.reactivex.Observer;
    import io.reactivex.android.schedulers.AndroidSchedulers;
    import io.reactivex.disposables.Disposable;
    import io.reactivex.schedulers.Schedulers;
    
    public class MainActivity extends AppCompatActivity {
        private TextView contentTxt;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            contentTxt = findViewById(R.id.text);
            findViewById(R.id.clickBtn).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dealEvent();
                }
            });
        }
    
        /*
        *  18/1/22  -  11:10
        *  处理事件
        */
        private void dealEvent() {
            Observable
                    .create(new ObservableOnSubscribe<Object>() {
                        @Override
                        public void subscribe(final ObservableEmitter<Object> observableEmitter) throws Exception {
                            new Thread(new Runnable() {
                                @Override
                                public void run() {
                                    String message = "哈哈哈哈";
                                    for (int i = 0; i < 10; i++) {
                                        try {
                                            Thread.sleep(500);
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                        message += i;
                                    }
                                    observableEmitter.onNext(message);
                                    observableEmitter.onComplete();
                                }
                            }).start();
                        }
                    })
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<Object>() {
    
                        @Override
                        public void onSubscribe(Disposable d) {
    
                        }
    
                        @Override
                        public void onNext(Object o) {
                            contentTxt.setText(o.toString());
                        }
    
                        @Override
                        public void onError(Throwable e) {
    
                        }
    
                        @Override
                        public void onComplete() {
    
                        }
                    });
        }
    }
    
    

    相关文章

      网友评论

          本文标题:rxjava和rxandroid简单的使用

          本文链接:https://www.haomeiwen.com/subject/gzklaxtx.html