-
Scheduler线程切换——eg:后台线程取数据,主线程展示
Flowable.just(1, 2, 3, 4)
.subscribeOn(Schedulers.io()) // 指定 subscribe() 发生在 IO 线程
.observeOn(AndroidSchedulers.mainThread()) // 指定 Subscriber 的回调发生在主线程
.subscribe(new Consumer<Integer>() {
@Override
public void accept(@NonNull Integer integer) throws Exception {
}
});
- CheckBox状态实时更新
private void checkBoxState(){
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
RxSharedPreferences rxPreferences = RxSharedPreferences.create(preferences);
Preference<Boolean> xxFunction = rxPreferences.getBoolean("isfirst", false);
checkBox.setChecked(xxFunction.get());
RxCompoundButton.checkedChanges(checkBox).subscribe(xxFunction.asConsumer());
}
-
输入框改变即网络请求(操作)——eg:输入投资金额计算收益、搜索关键系提醒
private void editTextChange() {
RxTextView.textChanges(edittext)
.debounce(1000, TimeUnit.MILLISECONDS)//当你敲完字停下来1秒就会执行下面语句
.filter(new Predicate<String>() {
@Override
public boolean test(String text) throws Exception {
if (text.isEmpty()) {
textViewResult.setText("");
return false;
} else {
return true;
}
}
})
.distinctUntilChanged()//用于防止重复执行得到相同结果
//下面这两个都是数据转换
//flatMap:当同时多个网络请求访问的时候,前面的网络数据会覆盖后面的网络数据
//switchMap:当同时多个网络请求访问的时候,会以最后一个发送请求为准,前面网路数据会被最后一个覆盖
.switchMap(new Function<CharSequence, ObservableSource<List<String>>>() {
@Override
public ObservableSource<List<String>> apply(@NonNull CharSequence charSequence) throws Exception {
//网络操作,获取我们需要的数据
List<String> list = new ArrayList<String>();
list.add("我在进行网络请求");
return Observable.just(list);
}
})
.subscribeOn(Schedulers.io()) //网络请求是在子线程的
.observeOn(AndroidSchedulers.mainThread())//界面更新在主线程
.subscribe(new Consumer<List<String>>() {
@Override
public void accept(@NonNull List<String> strings) throws Exception {
//网络请求结果
}
});
-
输入框过滤——eg:登录注册等常用过滤
private void combineLatestEvents() {
Flowable<CharSequence> numberChangeObservable = RxTextView.textChanges(edittextUser).skip(1).toFlowable(BackpressureStrategy.LATEST);
Flowable<CharSequence> passwordChangeObservable = RxTextView.textChanges(edittextPassword).skip(1).toFlowable(BackpressureStrategy.LATEST);
Flowable.combineLatest(
numberChangeObservable,
passwordChangeObservable,
new BiFunction<CharSequence, CharSequence, Boolean>() {
@Override
public Boolean apply(@NonNull CharSequence charSequence, @NonNull CharSequence charSequence2) throws Exception {
//进行输入框过滤判断
return true;
}
})
.subscribe(new Consumer<Boolean>() {
@Override
public void accept(@NonNull Boolean aBoolean) throws Exception {
if (aBoolean) { //过滤判断后相应的操作
} else {
}
}
});
}
-
倒计时——eg:启动页或发送验证码
public void onClick(View v) {
final long count = 3;
Observable.interval(0, 1, TimeUnit.SECONDS)//(long initialDelay, long period, TimeUnit unit)
//参数一:延时initialDelay秒;参数二:每隔period秒开始执行
.take(count + 1)//执行count + 1次(没有该方法不会停止)
.map(new Function<Long, Long>() {
@Override
public Long apply(@NonNull Long aLong) throws Exception {
return count - aLong;
}
})
.observeOn(AndroidSchedulers.mainThread())
.doOnSubscribe(new Consumer<Disposable>() {
@Override
public void accept(@NonNull Disposable disposable) throws Exception {
//进入倒计时
button.setEnabled(false);
button.setTextColor(Color.BLUE);
}
})
.subscribe(new Observer<Long>() {
@Override
public void onSubscribe(Disposable d) {}
@Override
public void onNext(Long aLong) {
button.setText( aLong + "S后重新发发送");
}
@Override
public void onError(Throwable e) {}
@Override
public void onComplete() {
button.setEnabled(true);
button.setTextColor(Color.RED);
button.setText("发送验证码");
}
});
}
- 防抖动
/**
* 防止抖动
*/
@NonNull
public static Observable<Object> clicks(@NonNull View view) {
return RxView.clicks(view).throttleFirst(1000, TimeUnit.MILLISECONDS, AndroidSchedulers.mainThread());
}