标签:class select 技术 执行 read number rom 官方 erp
The Debounce operator filters out items emitted by the source Observable that are rapidly followed by another emitted item.
public final Observable<T> debounce(long timeout, TimeUnit unit); // 默认执行线程 Schedulers.computation() public final Observable<T> debounce(long timeout, TimeUnit unit, Scheduler scheduler);
快速点击按钮,执行某个操作。
比如美团APP中的选择套餐:由左图的0份快速点击到右图的7份,然后根据选中份数计算总价。
// 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来解决。
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操作
// 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
标签:class select 技术 执行 read number rom 官方 erp
原文地址:http://www.cnblogs.com/ruyingxiangsui/p/6082777.html