美文网首页
Android View防抖功能

Android View防抖功能

作者: Tyson_Wu | 来源:发表于2023-02-21 16:18 被阅读0次

    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()
    }
    

    相关文章

      网友评论

          本文标题:Android View防抖功能

          本文链接:https://www.haomeiwen.com/subject/vmxokdtx.html