Multiple Methods for Android to Deal with View Repeated Clicks
- 2021-12-11 18:51:15
- OfStack
1. Abstract classes
// Realization View.OnClickListener Interface
public abstract class OnThrottleClickListener implements View.OnClickListener {
private static final String TAG = "OnThrottleClickListener";
private static final int SKIP_DURATION = 300;//milliseconds
private long mLastClickTime;
@Override
public void onClick(View v) {
if (System.currentTimeMillis() - mLastClickTime > SKIP_DURATION) {
onThrottleClick(v);
mLastClickTime = System.currentTimeMillis();
} else {
Log.e(TAG, "OnThrottleClickListener: Repeat clicking ");
}
}
protected abstract void onThrottleClick(View v);
}
// Substitute new View.OnClickListener() Use
id_tv_1.setOnClickListener(new OnThrottleClickListener() {
@Override
protected void onThrottleClick(View v) {
Log.e(TAG, "onClick: OnThrottleClickListener ");
}
});
2. Proxy mode
// Proxy class implementation View.OnClickListener Interface
public class ThrottleClickProxy implements View.OnClickListener {
private static final String TAG = "ThrottleClickProxy";
private static final int SKIP_DURATION = 300;//milliseconds
private long mLastClickTime;
// Source object
private View.OnClickListener mOriginListener;
// Structure
public ThrottleClickProxy(View.OnClickListener mOriginListener) {
this.mOriginListener = mOriginListener;
}
@Override
public void onClick(View v) {
if (System.currentTimeMillis() - mLastClickTime >= SKIP_DURATION) {
mOriginListener.onClick(v);
mLastClickTime = System.currentTimeMillis();
} else {
Log.e(TAG, "ThrottleClickProxy: Repeat clicking ");
}
}
}
// Use
id_tv_2.setOnClickListener(new ThrottleClickProxy(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.e(TAG, "onClick: ThrottleClickProxy ");
}
}));
3. RxBinding of RxAndroid
implementation 'com.jakewharton.rxbinding3:rxbinding:3.0.0-alpha1'
RxView.clicks(id_tv_3)
.throttleFirst(300, TimeUnit.MILLISECONDS)
.subscribe(new Consumer<Unit>() {
@Override
public void accept(Unit unit) throws Exception {
Log.e(TAG, "onClick: throttleFirst ");
}
});
4. Eclipse AspectJ of AOP
// Adopt AspectJX To quickly configure Eclipse AspectJ
//project
dependencies {
classpath "com.android.tools.build:gradle:4.1.2"
//add
classpath 'com.hujiang.aspectjx:gradle-android-plugin-aspectjx:2.0.10'
}
plugins {
id 'com.android.application'
//add
id 'android-aspectjx'
}
//module
dependencies {
//add
implementation 'org.aspectj:aspectjrt:1.9.5'
}
@Aspect
public class ThrottleClickAspect {
private static final String TAG = "ThrottleClickAspect";
private static final int SKIP_DURATION = 3000;
private long mLastClickTime;
// All android.view.View.OnClickListener.onClick Methods will be woven into, like the first 3 Square component RxView.clicks() Li Yehui
@Around("execution(* android.view.View.OnClickListener.onClick(..))")
public void aroundViewOnClick(ProceedingJoinPoint joinPoint) throws Throwable {
if (System.currentTimeMillis() - mLastClickTime >= SKIP_DURATION) {
joinPoint.proceed();
mLastClickTime = System.currentTimeMillis();
} else {
Log.e(TAG, "ThrottleClickAspect: Repeat clicking ");
}
}
}
The code is non-intrusive and takes effect directly
Above is Android to deal with View repeated clicks of a variety of methods of details, more about Android to deal with View repeated clicks of information please pay attention to other related articles on this site!