利用RxJava防止控件二次点击(防抖)
2018-08-25 本文已影响215人
岁月无痕灬灬
app的build.gradle,导入Rxjava库
implementation : "io.reactivex:rxjava:1.2.10"
1.新建xxxSubscribe类实现Observable.OnSubscribe<View>
private static final String TAG = "ClickSubscribe";
private Subscriber mSubscriber;
public ClickSubscribe(View view) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
LogUtils.e(TAG, v.getClass().getSimpleName() + "我被点击了");
mSubscriber.onNext(v);
}
});
}
@Override
public void call(Subscriber<? super View> subscriber) {
mSubscriber = subscriber;
}
2.运用操作符
throttleFirst(time,TimeUnit.SECONDS)
如下方式
Observable.create(new ClickSubscribe(nextBtn))
.throttleFirst(2, TimeUnit.SECONDS)
.subscribe(new Action1<View>() {
//todo dothing
}