RX-JAVA&RX-Android的使用方法
rxjava的创建方式,主要有create,just,fromArray
Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(@NonNull ObservableEmitter<String> e) throws Exception {
e.onNext("aaaa");
e.onError(new Exception("报错啦,啊哈哈哈哈"));
e.onComplete();
}
}).subscribe(new DefaultObserver<String>() {
@Override
public void onNext(@NonNull String s) {
Log.e(TAG, "onNext: " + s );
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "onError: ", e);
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: " );
}
});
Observable.just("哈哈哈哈","666").subscribe(new DefaultObserver<String>() {
@Override
public void onNext(@NonNull String s) {
Log.e("HHH", "onNext: " + s );
}
@Override
public void onError(@NonNull Throwable e) {
Log.e("HHH", "onError: ");
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: " );
}
});
Observable.fromArray("1","2","3","4").subscribe(new DisposableObserver<String>() {
@Override
public void onNext(@NonNull String s) {
Log.e(TAG, "onNext: " + s );
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "onError: ");
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: ");
}
});
这基本 用法就这么简单
map,flatMap操作符,
Observable.just("123").map(new Function<String, Integer>() {
@Override
public Integer apply(@NonNull String s) throws Exception {
return Integer.valueOf(s);
}
}).subscribe(new DefaultObserver<Integer>() {
@Override
public void onNext(@NonNull Integer integer) {
Log.e(TAG, "onNext: " + integer );
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "onError: " );
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: " );
}
});
Observable
.just("asas")
.flatMap(new Function<String, ObservableSource<? extends String>>() {
@Override
public ObservableSource<? extends String> apply(@NonNull String s) throws Exception {
return Observable.just("one", "two", "three", "four", "five");
}
})
.subscribe(new DefaultObserver<String>() {
@Override
public void onNext(@NonNull String s) {
Log.e(TAG, "onNext: " + s);
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "onError: ");
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: ");
}
});
过滤器
Observable.just("123","4","5","6","4","5").filter(new Predicate<String>() {
@Override
public boolean test(@NonNull String s) throws Exception {
Log.e(TAG, "test: " + s );
// 为true时才返回该数据
return true;
}
}).subscribe(new DefaultObserver<String>() {
@Override
public void onNext(@NonNull String s) {
Log.e(TAG, "onNext: " + s );
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "onError: " );
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: " );
}
});
// 获取其中的第几位
Observable.just("123","4","5","6","4","5").elementAt(1).subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.e(TAG, "accept: " + s );
}
});
// 去掉重复的数据
Observable.just("123","4","5","6","4","5").distinct().subscribe(new Consumer<String>() {
@Override
public void accept(String s) throws Exception {
Log.e(TAG, "accept: " + s);
}
});
更多参考: http://reactivex.io/documentation/operators.html#filtering
处理
Observable<String> observable1 = Observable.just("1", "2","8","0");
Observable<String> observable2 = Observable.just("3", "4", "6");
// 将两个合并
Observable.zip(observable1, observable2, new BiFunction<String, String, Integer>() {
@Override
public Integer apply(@NonNull String s, @NonNull String s2) throws Exception {
// 执行次数以少的为准
Log.e(TAG, "apply: " + s +"===="+ s2 );
return 0;
}
}).subscribe(new DefaultObserver<Integer>() {
@Override
public void onNext(@NonNull Integer s) {
Log.e(TAG, "onNext: " + s );
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: " );
}
});
// 排放
Observable<String> observable1 = Observable.just("1", "2","8","0");
Observable<String> observable2 = Observable.just("3", "4", "6");
// 将两个并列排放 下面打印出 : 12380346
Observable.merge(observable1, observable2).subscribe(new DefaultObserver<String>() {
@Override
public void onNext(@NonNull String s) {
Log.e(TAG, "onNext: " + s );
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: " );
}
});
// 在。。。之前加
Observable<String> observable1 = Observable.just("1", "2","8","0");
Observable<String> observable2 = Observable.just("3", "4", "6");
// 在observable1的前面加上observable2返回数据如:3461280
observable1.startWith(observable2).subscribe(new DefaultObserver<String>() {
@Override
public void onNext(@NonNull String s) {
Log.e(TAG, "onNext: " + s );
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
// observable1的最后一项依次与observable2合并
Observable<String> observable1 = Observable.just("1", "2");
Observable<String> observable2 = Observable.just("3", "4", "6");
// 返回 2=3 2=4 2=6
Observable.combineLatest(observable1, observable2, new BiFunction<String, String, String>() {
@Override
public String apply(@NonNull String s, @NonNull String s2) throws Exception {
return s+"="+s2;
}
}).subscribe(new DefaultObserver<String>() {
@Override
public void onNext(@NonNull String s) {
Log.e(TAG, "onNext: " + s );
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: " );
}
});
Schedulers线程控制
Observable.just(Environment.getExternalStorageDirectory()+File.separator+"image.jpg") // 由.subscribeOn(Schedulers.io())指定线程
.subscribeOn(Schedulers.io())
.map(new Function<String, Bitmap>() {
@Override
public Bitmap apply(@NonNull String s) throws Exception {
Log.e(TAG, "apply: " + Thread.currentThread().getName() );
Bitmap bitmap = BitmapFactory.decodeFile(s);
return bitmap;
}
})
.observeOn(AndroidSchedulers.mainThread()) // 观察者回调到main线程上
.subscribe(new DefaultObserver<Bitmap>() {
@Override
public void onNext(@NonNull Bitmap bitmap) {
Log.e(TAG, "onNext: " + Thread.currentThread().getName() );
imageView.setImageBitmap(bitmap);
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
结束
private final CompositeDisposable disposables = new CompositeDisposable();
disposables.add(Observable.just(Environment.getExternalStorageDirectory() + File.separator + "image.jpg")
.subscribeOn(Schedulers.newThread())
.map(new Function<String, Bitmap>() {
@Override
public Bitmap apply(@NonNull String s) throws Exception {
Log.e(TAG, "apply: " + Thread.currentThread().getName() );
int i = 0;
while (i<10) { // 在结束的时候还是会执行这里
i++;
Log.e(TAG, "apply: " + i );
SystemClock.sleep(1000);
}
return BitmapFactory.decodeFile(s);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(new DisposableObserver<Bitmap>() {
@Override
public void onNext(@NonNull Bitmap bitmap) {
Log.e(TAG, "onNext: " );
imageView.setImageBitmap(bitmap);
}
@Override
public void onError(@NonNull Throwable e) {
Log.e(TAG, "onError: ", e );
}
@Override
public void onComplete() {
Log.e(TAG, "onComplete: " );
}
}));