前言
- 异步操作都可以用RxJava
- 延时,轮询也可以用RxJava
- 数据类型经过操作转变也可以用RxJava
- 联合判断可以用RxJava
- 防快速双击也可以用RxJava
总结Rxjava是个好东西,用用更健康
一、延迟xx秒后执行
应用场景:启动页
timer操作符
示例代码:
Observable.timer(1500, TimeUnit.MILLISECONDS)
.compose(this.<Long>bindToLifecycle())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
//倒计时完做操作,有广告图交给广告图
if (flAd.getVisibility() != View.VISIBLE) {
toNext();
}
}
}, new Consumer<Throwable>() {
@Override
public void accept(Throwable throwable) throws Exception {
toNext();
}
});
二、xx秒倒计时
应用场景:验证码倒计时
interval和take操作符
Observable
.interval(1, TimeUnit.SECONDS, AndroidSchedulers.mainThread())
.take(second)
.subscribe(nextObserver,errorObserver,completeObserver);
三、每隔多少s轮播一次
应用场景:轮播banner
interval操作符
Observable.interval(3, TimeUnit.SECONDS)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe
四、字符串经过操作转为字符串(类型转换)
应用场景:支付宝支付
just和map操作符
Observable.just(regOrderResult.getData().getOrder())
.map(new Function<String, String>() {
@Override
public String apply(String orderInfo) throws Exception {
//用户在商户app内部点击付款,是否需要一个loading做为在钱包唤起之前的过渡,这个值设置为true
return new PayTask(activity).pay(orderInfo, true);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<String>() {
@Override
public void accept(String resultStatus) throws Exception {
PayResult payResult = new PayResult(resultStatus);
switch (payResult.getResultStatus()) {
case AliPayResultStatus.PAY_SUCCESS:
ToastUtils.show(R.string.pay_success);
//通知接口支付成功
presenter.alipayVerify(new VerifyBody(InfoUtils.getUserId(), payResult.getResultStatus(), payResult.getResult(), regOrderResult.getData().getExtraParam()));
IntentUtils.startMainActivity(activity);
break;
case AliPayResultStatus.PAY_PROCESSING:
case AliPayResultStatus.PAY_UNKNOWN:
//支付可能成功,要接口去查询
ToastUtils.show(R.string.pay_fail);
presenter.alipayVerify(new VerifyBody(InfoUtils.getUserId(), payResult.getResultStatus(), payResult.getResult(), regOrderResult.getData().getExtraParam()));
break;
default:
ToastUtils.show(R.string.pay_fail);
//通知接口支付失败,取消订单
presenter.orderCancel(new CancelBody(regOrderResult.getData().getExtraParam()));
}
}
});
五、联合判断
应用场景:多个输入框都填写才启用下一步按钮
combineLatest操作符
val jineObservable = RxTextView.textChanges(et_jine).skip(1)
val cashOutNameObservable = RxTextView.textChanges(et_commission_cash_out_name).skip(1)
Observable.combineLatest(jineObservable, cashOutNameObservable,object : BiFunction<CharSequence?, CharSequence?, Boolean> {
override fun apply(t1: CharSequence, t2: CharSequence): Boolean {
money = t1.toString()
name = t2.toString()
val isUserNameValid: Boolean = !TextUtils.isEmpty(money)
val isUserAgeValid: Boolean = !TextUtils.isEmpty(name)
/*
* 步骤5:返回信息 = 联合判断,即2个信息同时已填写,"提交按钮"才可点击
**/return isUserNameValid && isUserAgeValid && is_binding == "2"
}
}).subscribe(object :Consumer<Boolean>{
override fun accept(t: Boolean) {
ViewUtils.setEnable(next_button, t)
}
})
六、防双击操作
应用场景:2s内只能点击一次按钮(应该说只响应一次)
throttleFirst操作符
RxView.clicks(sign_button)
.throttleFirst(2,TimeUnit.SECONDS)
.subscribe(object :Consumer<Any>{
override fun accept(t: Any?) {
}
})
网友评论