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


相关推荐

  • stn  pytorch[通俗易懂]

    stn  pytorch[通俗易懂]#-*-coding:utf-8-*-"""SpatialTransformerNetworksTutorial=====================================**Author**:`GhassenHAMROUNI<https://github.com/GHamrouni>`_..figure::/_static/img/…

    2022年10月19日
    3
  • 网站检测空链、死链工具(Xenu)

    网站检测空链、死链工具(Xenu)网站常用检测空链、死链工具网站的链接一般都成千上万,如果存在大量的空链接将大大的影响用户体验,怎样有效检测无效链接。下面是比较常用的几种简单工具。一、Xenu(Xenu’sLinkSleuth)1、文件→检测网址,打开如下图,输入根网址,点击确定即可。如果想检测本地html文件可点击本地文件然后导入。2、点击确定,开始自…

    2022年7月22日
    38
  • java中定义常量_形参可以是表达式吗

    java中定义常量_形参可以是表达式吗如here所述,javac和其他Java编译器可能为条件为“ConstantExpression”的if语句提供代码消除功能.如果我的代码使用依赖于不同包中定义的其他常量表达式的常量表达式,那么这将如何影响?例如,假设我在相应的指定包中有以下类:packagefoo;publicclassFoo{publicstaticfinalbooleanCONDITION=false;…

    2022年9月29日
    4
  • random.randint()用法

    random.randint()用法函数功能:random.randint(参数1,参数2)参数1、参数2必须是整数函数返回参数1和参数2之间的任意整数举例:importrandomresult=random.randint(1,10)print(“result:”,result)输出:result:6

    2022年6月10日
    116
  • 垂直同步、三重缓冲、freesync

    垂直同步、三重缓冲、freesync一、垂直同步60Hz显示器,开启垂直同步后,就会锁60了;作用:1、解决画面撕裂现象,不会出现缓冲没画完被复写的情况;2、解决错帧现象;游戏更流畅;3、强制每帧间隔完全一样,这样因为帧生成时间不平滑导致的不流畅也会解决弊端:鼠标反馈,移动鼠标,电脑收到消息把移动鼠标输出给显卡,显卡收到后把鼠标移动画面输出给显示器,所有请求不会被延后,延迟只是电路延迟。但开启垂直同步,显卡绘制完后缓冲后,显示器还没有显示器完前缓冲,显卡等着,鼠标移动指令和显卡一起等着,直到显示…

    2022年6月9日
    43
  • C多线程操作UI

    C多线程操作UIusingSystem usingSystem Collections Generic usingSystem ComponentMod usingSystem Data usingSystem Drawing usingSystem Linq usingSystem Text usingSystem Threading

    2025年11月15日
    4

发表回复

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

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