Rxjava2之rxandroid基本用法

来源:互联网 发布:艾蕾 知乎 编辑:程序博客网 时间:2024/06/05 02:55

rxAndroid的github地址https://github.com/ReactiveX/RxAndroid 
配置:module下

compile 'io.reactivex.rxjava2:rxandroid:2.0.1'compile 'io.reactivex.rxjava2:rxjava:2.0.5'
  • 1
  • 2
  • 3

这里主要介绍基本的使用以及Action和Function的一些变动

以下是RxJava的标准使用

private void normalCreate() {        //创建观察者,subscriber或observer        Observer<String> observer=new Observer<String>() {            @Override            /**             * Provides the Observer with the means of cancelling (disposing) the             * connection (channel) with the Observable in both             * synchronous (from within {@link #onNext(Object)}) and asynchronous manner.             * @param d the Disposable instance whose {@link Disposable#dispose()} can             * be called anytime to cancel the connection             * @since 2.0             */            public void onSubscribe(Disposable d) {                //可用于取消订阅                d.dispose();                //还可以判断是否处于取消状态                //boolean b=d.isDisposed();            }    //一般需要展示            @Override            public void onNext(String s) {        Log.e("TAG", "onNext: "+s );            }            @Override            public void onError(Throwable e) {            }            @Override            public void onComplete() {            }        };        //创建被观察者        //和RxJava1.0相比,他们都增加了throws Exception,也就是说,在这些方法做某些操作就不需要try-catch 。        Observable<String> observable=Observable.create(new ObservableOnSubscribe<String>() {            @Override            //将事件发射出去,持有观察者的对象            public void subscribe(ObservableEmitter<String> e) throws Exception {                e.onNext("第一次调用");                e.onNext("第二次调用");                e.onNext("第三次调用");                e.onComplete();            }        });//确立订阅关系        observable.subscribe(observer);    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
private void mapDemo() {        //在2.0后命名规则有了改变        //Action1--------Consumer一个参数的        //Action2--------BiConsumer两个参数的        //而Function的也一样        Observable.just("图片存放路径").map(new Function<String, Bitmap>() {            @Override            public Bitmap apply(String s) throws Exception {                return getBitmapFromFile(s);            }        }).subscribeOn(Schedules.io())//指定 subscribe() 事件发送发生在 IO 线程        .observeOn(AndroidSchedulers.mainThread())//指定 Subscriber 的回调处理发生在主线程                //这里的Action一个参数的改为COnsumer                .subscribe(new Consumer<Bitmap>() {                    @Override                    public void accept(Bitmap bitmap) throws Exception {                        iv.setImageBitmap(bitmap);                    }                });    }