RxView可以做防抖功能
//多次点击只产生一次请求,防抖动
RxView.clicks(btn_order_pay)
.throttleFirst(1, TimeUnit.SECONDS)
.subscribe {
val param = PayParam()
param.apply {
orderSeq = mOrderSeq
refundChangeTotalFee = refundChangeTotalFee
}
mViewModel.payRefundChangeFee(param)
}
点击事件计时防抖
/**
* @name: GPS Camera
* @author: TysonWu
* @time: 2023/2/22 15:41
* @version:
* @description:点击事件防抖工具类
*/
public class AntiShakeUtils {
private static final long COLD_TIME_CLICK = 1000L;
private static final long COLD_TIME_LONG_CLICK = 1500L;
private static long sLastResponseClick = 0;
public static boolean canResponseClick() {
long currentTime = SystemClock.elapsedRealtime();
return (currentTime - sLastResponseClick) >= COLD_TIME_CLICK;
}
public static void updateLastClickTime() {
sLastResponseClick = SystemClock.elapsedRealtime();
}
public static boolean canResponseLongClick() {
long currentTime = SystemClock.elapsedRealtime();
return (currentTime - sLastResponseClick) >= COLD_TIME_LONG_CLICK;
}
}
使用
if (!AntiShakeUtils.canResponseClick()) {
return
} else {
AntiShakeUtils.updateLastClickTime()
}
网友评论