美文网首页
Android 利用AOP实现防抖功能

Android 利用AOP实现防抖功能

作者: Vitaming | 来源:发表于2021-06-29 14:35 被阅读0次
    @Aspect
    public class ClickAspect {
        
        private static LruCache<Integer, Long> clickCache = new LruCache<>(5);
    
        @Pointcut("execution(@ClickSingle * * (..)) && @annotation(clickSingle)")
        public void click(ClickSingle clickSingle) {
        }
    
    
        @Around("click(clickSingle)")
        public void getClickPoint(ProceedingJoinPoint proceedingJoinPoint, ClickSingle clickSingle) throws Throwable {
            if (clickCache.get(proceedingJoinPoint.getSignature().hashCode()) != null) {
                if (clickCache.get(proceedingJoinPoint.getSignature().hashCode()) + clickSingle.value() <= System.currentTimeMillis()) {
                    clickCache.put(proceedingJoinPoint.getSignature().hashCode(), System.currentTimeMillis());
                    proceedingJoinPoint.proceed();
                }
            } else {
                clickCache.put(proceedingJoinPoint.getSignature().hashCode(), System.currentTimeMillis());
                proceedingJoinPoint.proceed();
            }
        }
    }
    
    
    @Target({ElementType.METHOD})
    @Retention(RetentionPolicy.RUNTIME)
    public @interface ClickSingle {
        int value() default 150;
    }
    
    

    相关文章

      网友评论

          本文标题:Android 利用AOP实现防抖功能

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