配置: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);
}
private void mapDemo() { //在2.0后命名规则有了改变 //Action1--------Consumer一个参数的 //Action2--------BiConsumer两个参数的