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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • thinkphp漏洞检测工具_thinkphp渗透

    thinkphp漏洞检测工具_thinkphp渗透0x00前言由于框架对控制器名没有进行足够的检测会导致在没有开启强制路由的情况下可能的getshell漏洞,受影响的版本包括5.0和5.1版本,推荐尽快更新到最新版本。0x01影响范围5.x<5.1.31,<=5.0.230×02漏洞分析Thinkphpv5.0.x补丁地址:https://github.com/top-think/framework/com…

    2025年8月6日
    2
  • 复制粘贴不到远程桌面_本地不能复制粘贴到服务器

    复制粘贴不到远程桌面_本地不能复制粘贴到服务器在远程服务器上打开任务管理器,在进程里面找到rdpclip进程(或者剪贴板监视器),点击“结束进程”打卡DOS命令符,输入rdpclip后,确认,从新打开远程剪贴服务。

    2022年8月20日
    6
  • html psd设计图,根据PSD登陆页面设计稿切图制作HTML网页全过程

    html psd设计图,根据PSD登陆页面设计稿切图制作HTML网页全过程切图的目的是把PSD设计稿转换为HTML页面。记得自己当时学习切图时,网上切图相关的文章是不少,但是大都是讲怎么使用切片工具把一张图片分割成多张图片,然后存储为Web使用格式,并且都比较简单和零碎,并不能满足我的要求。因此只能自己不断尝试,今天分享给大家“如何根据PSD设计稿通过PS切图和DIV+CSS布实现HTML页面”,希望能帮助到大家。实例:下面通过一个简单的登陆页面PSD设计稿来演示转化为…

    2022年6月10日
    88
  • SQL NOT NULL 约束[通俗易懂]

    SQL NOT NULL 约束[通俗易懂]SQLNOTNULL约束在默认的情况下,表的列接受NULL值。SQLNOTNULL约束NOTNULL约束强制列不接受NULL值。NOTNULL约束强制字段始终包含值。这意味着,如果不向字段添加值,就无法插入新记录或者更新记录。下面的SQL强制“P_Id”列和“LastName”列不接受NULL值:CREATETABLEPersons(…

    2022年5月29日
    41
  • centos安装php7.18注意

    centos安装php7.18注意

    2021年10月27日
    45
  • CentOS7使用firewalld打开关闭防火墙与端口

    CentOS7使用firewalld打开关闭防火墙与端口

    2021年10月18日
    39

发表回复

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

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