使用aspectJ方式 AspectJ处理重复点击过快点击事件
1.项目的gradle导入
dependencies {
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
2.app的gradle添加
apply plugin: 'com.hujiang.android-aspectjx'
3.增加ClickGap注解类
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.CLASS)
@Target({ElementType.CONSTRUCTOR,ElementType.METHOD})
public @interface ClickGap {
}
4.SingleClickAspect
@Aspect
public class SingleClickAspect {
private static long clickGapTime = 0;
public static final int CLICK_GAP_RESPONSE = 500;//500ms内不响应
//判断是否应该执行,true执行,false不执行
protected boolean clickGapFilter(){
long currentTimeMillis = System.currentTimeMillis();
if(currentTimeMillis-clickGapTime< CLICK_GAP_RESPONSE){
return false;
}
clickGapTime = currentTimeMillis;
return true;
}
@Around("execution(@ClickGap * *(..))")
public void clickGap(ProceedingJoinPoint proceedingJoinPoint)throws Throwable {
if(clickGapFilter() == true) {
proceedingJoinPoint.proceed();
}
}
}
5.使用方法
btn.setOnClickListener(new View.OnClickListener() {
@Override
@ClickGap
public void onClick(View view) {
}
});
6.有个坑,我的项目导入了阿里云推送,每次初始化都crash,显示找不到包,
在app的gradle中添加,排除com.alibaba这个包
kolin版本太高,排除versions.9
leakcanary报错,排除leakcanary
android {
aspectjx {
exclude 'com.alibaba'
exclude 'versions.9'
exclude 'leakcanary'
}
}