java实现debounce_Rxjava debounce 操作符

java实现debounce_Rxjava debounce 操作符Debounce1.官方定义onlyemitanitemfromanObservableifaparticulartimespanhaspassedwithoutitemittinganotheritemTheDebounceoperatorfiltersoutitemsemittedbythesourceObservablethatare…

大家好,又见面了,我是你们的朋友全栈君。

Debounce

1.官方定义

only emit an item from an Observable if a particular timespan has passed without it emitting another item

79a2fbba080398160f0ec45ac170a3b9.png

The Debounce operator filters out items emitted by the source Observable that are rapidly followed by another emitted item.

2.API

public final Observable debounce(long timeout, TimeUnit unit); //默认执行线程 Schedulers.computation()

public final Observable debounce(long timeout, TimeUnit unit, Scheduler scheduler);

3.Android中使用场景

快速点击按钮,执行某个操作。

b201f42e2d80259753175d0604741c86.png

edb54b757656e3a1cff79e73c4751b5c.png

比如美团APP中的选择套餐:由左图的0份快速点击到右图的7份,然后根据选中份数计算总价。

4.代码实现一

//NumberPickerView.java

plusView.setOnClickListener(v- >{

selectCount++;

countTv.setText(selectCount+ “”);

onChangeListener.onChange(dealId, selectCount);//dealId为当前套餐的id

});public interfaceOnChangeListener {

onChange(int dealId, intselectCount);

}

//activity

numberPickerView.setOnChangeListener((dealId, selcetCount)->{

calculateDealPrice(dealId, selectCount);

});private calculateDealPrice(int dealId, intselectCount) {

…//计算价格

}

对于这种快速点击,我们其实需要的是对第7次进行计算,中间的一系列暂存态是没必要计算的,使用debounce来解决。

5.代码实现二:增加debounce操作

RxView.clicks(plusView)

.map(aVoid->{

selectCount++;

countTv.setText(selectCount+ “”);returnselectCount;

}

.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 subject =PublishSubject.create();

numberPickerView.setOnChangeListener((dealId, selectCount)->{

subject.onNext(newSelectParams(dealId, selectCount));

});

subject.debounce(400, TimeUnit.MILLISECONDS)

.observeOn(AndroidSchedulers.mainThread())

.subscribe(selectParams->calculateDealPrice(selectParams.dealId, selectParams.selectCount), Throwable::printStackTrace);classSelectParams {intdealId;intselectCount;

SelectParams(int dealId, intselectCont) {this.dealId =dealId;this.selectCount =selectCount;

}

}private calculateDealPrice(int dealId, intselectCount) {

…//计算价格

}

此时NumberPickerView不再依赖第三方库,适用性提高。

参考:http://reactivex.io/documentation/operators.html

原文:http://www.cnblogs.com/ruyingxiangsui/p/6082777.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/151572.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 利用公网Msf+MS17010跨网段攻击内网(不详细立马关站)「建议收藏」

    利用公网Msf+MS17010跨网段攻击内网(不详细立马关站)「建议收藏」前言其实很多小白在对利用msf进行ms17010攻击的时候总是不成功,其实这都是因为网上大部分的文章都是写的内网对内网(192.168.1.2–>192.168.1.3)这样的案例,今天写了一下利用公网的MSF直接攻击其他服务器内网的案例。准备工作1.安装了Msf的公网主机+Ubuntu系统2.一台其他服务器内网存在445漏洞3.细心,细心,细心,钻研,钻研!!!…

    2022年5月31日
    128
  • 神器 Codelf !

    神器 Codelf !公众号关注“五分钟学算法”设为“星标”,带你挖掘更多开发神器!大家好,我是小G。程序员最头疼的事情除了头发以外就是给变量或函数命名,一开始学编程语言的时候还可以abc、a1、x2…

    2022年6月4日
    31
  • 字符串的赋值

    字符串的赋值我们可以知道 char p helo 这种字符串的赋值方式是完全没有问题的 要理解这种赋值方式 我们首先得理解双引号 特別注意 这个是双引号 不要赋值的时候给弄了个单引号 在这个语句中做了什么工作 双引号主要做了 3 个工作 分别 1 申请了空间 在常量区 存放了字符串 2 在字符串尾加上了 0 3 返回地下面是转载 学了这么多年的 C 语言 突然发现连字符串赋值都出错 chara 10 怎么给这个数组赋值呢 1 定义的时候直接用字符串赋值 chara 10 hello 注意

    2025年6月16日
    0
  • Spring MVC 3 深入总结

    Spring MVC 3 深入总结

    2021年12月4日
    45
  • 拉链表的实现过程[通俗易懂]

    拉链表的实现过程[通俗易懂]拉链表的优势我就不说了,具体请参考百度百科:拉链表-百度百科推荐一个比较详细的参考文章:拉链表示例主要总结一下实现过程:分析:拉链表就是用来存储变化的数据的,每一份数据都有对应的有效期,我们需要进行的操作就是将变动的数据进行新增,同时将变动对应的前一条数据的有效期进行变更。说明:一般都是今天处理昨天的数据,本文所说的当天为所处理的数据的产生的当天。在这之前需要熟悉一下需要用到的表:表1:订单表(记录原始的数据)表2:增量数据表(记录每日变更的数据)表3:历史拉链表(我们要得到的就是这张表

    2022年10月9日
    0
  • 工作日两个日期之间的数

    工作日两个日期之间的数

    2021年9月9日
    65

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号