首页 > 代码库 > Rxjava debounce 操作符
Rxjava debounce 操作符
Debounce
1.官方定义
only emit an item from an Observable if a particular timespan has passed without it emitting another item
The Debounce operator filters out items emitted by the source Observable that are rapidly followed by another emitted item.
2.API
public final Observable<T> debounce(long timeout, TimeUnit unit); // 默认执行线程 Schedulers.computation() public final Observable<T> debounce(long timeout, TimeUnit unit, Scheduler scheduler);
3.Android中使用场景
快速点击按钮,执行某个操作。
比如美团APP中的选择套餐:由左图的0份快速点击到右图的7份,然后根据选中份数计算总价。
4.代码实现一
// NumberPickerView.java ... plusView.setOnClickListener(v - > { selectCount++; countTv.setText(selectCount + ""); onChangeListener.onChange(dealId, selectCount); // dealId为当前套餐的id }); public interface OnChangeListener { onChange(int dealId, int selectCount); }
// activity ... numberPickerView.setOnChangeListener((dealId, selcetCount) -> { calculateDealPrice(dealId, selectCount); }); private calculateDealPrice(int dealId, int selectCount) { ... // 计算价格 }
对于这种快速点击,我们其实需要的是对第7次进行计算,中间的一系列暂存态是没必要计算的,使用debounce来解决。
5.代码实现二:增加debounce操作
RxView.clicks(plusView) .map(aVoid -> { selectCount++; countTv.setText(selectCount + ""); return selectCount; } .debounce(400, TimeUnit.MILLISECONDS)) .observeOn(AndroidSchedulers.mainThread()) .subcribe(count -> onChangeListener.onChange(dealId, selectCount), Throwable::printStackTrace);
缺点:
1.NumberPickerView依赖了 com.jakewharton.rxbinding:rxbinding:x.x.x
2.NumberPickerView中plusView被强制增加了400ms的debounce操作
5.代码实现三:将debounce操作移出NumberPickerView
// NumberPickerView.java ... plusView.setOnClickListener(v - > { selectCount++; countTv.setText(selectCount + ""); onChangeListener.onChange(dealId, selectCount); // dealId为当前套餐的id });
// activity ... PublishSubject<SelectParams> subject = PublishSubject.create(); numberPickerView.setOnChangeListener((dealId, selectCount) -> { subject.onNext(new SelectParams(dealId, selectCount)); }); subject.debounce(400, TimeUnit.MILLISECONDS) .observeOn(AndroidSchedulers.mainThread()) .subscribe(selectParams -> calculateDealPrice(selectParams.dealId, selectParams.selectCount), Throwable::printStackTrace); class SelectParams { int dealId; int selectCount; SelectParams(int dealId, int selectCont) { this.dealId = dealId; this.selectCount = selectCount; } } private calculateDealPrice(int dealId, int selectCount) { ... // 计算价格 }
此时NumberPickerView不再依赖第三方库,适用性提高。
参考:http://reactivex.io/documentation/operators.html
Rxjava debounce 操作符
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。