您好,登录后才能下订单哦!
这篇文章主要介绍了Android中View事件防抖的处理方案,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
侵入式防抖处理(NoShakeClickListener) implements View.OnClickListener
无侵入式防抖处理(NoShakeClickListener2) not implements View.OnClickListener
1. 既适用于单个View事件防抖, 也适用于Adapter中ItemView事件防抖
2. 如果事件为跳转到新的Activity, 该Activity启动模型应为android:launchMode="singleTop"
public abstract class NoShakeClickListener implements View.OnClickListener { private long mTimeInterval = 500L; /** * 最近一次点击的时间 */ private long mLastClickTime; /** * 最近一次点击的控件ID */ private int mLastClickViewId; public NoShakeClickListener() { } public NoShakeClickListener(long interval) { this.mTimeInterval = interval; } @Override public void onClick(View v) { final boolean isFastClick = isFastDoubleClick(v, this.mTimeInterval); if (isFastClick) { onFastClick(v); } else { onSingleClick(v); } } /** * 是否是快速点击 * * @param v 点击的控件 * @param interval 时间间期(毫秒) * @return true:是,false:不是 */ private boolean isFastDoubleClick(View v, long interval) { int viewId = v.getId(); long nowTime = System.currentTimeMillis(); long timeInterval = Math.abs(nowTime - mLastClickTime); if (timeInterval < interval && viewId == mLastClickViewId) { // 快速点击事件 return true; } else { // 单次点击事件 mLastClickTime = nowTime; mLastClickViewId = viewId; return false; } } protected void onFastClick(View v) {} protected abstract void onSingleClick(View v); }
可简写为
public abstract class NoShakeListener implements OnClickListener { private long mLastClickTime = 0; private boolean isFastDoubleClick() { long nowTime = System.currentTimeMillis(); if (Math.abs(nowTime - mLastClickTime) < 500) { return true; // 快速点击事件 } else { mLastClickTime = nowTime; return false; // 单次点击事件 } } @Override public void onClick(View v) { if (isFastDoubleClick()) { onFastClick(v); } else { onSingleClick(v); } } protected void onFastClick(View v) { } protected abstract void onSingleClick(View v); }
abstract class NoShakeClickListener @JvmOverloads constructor(interval: Long = 500L) : View.OnClickListener { private var mTimeInterval = 500L private var mLastClickTime: Long = 0 //最近一次点击的时间 private var mLastClickViewId = 0 //最近一次点击的控件ID init { mTimeInterval = interval } override fun onClick(v: View) { if (isFastDoubleClick(v, mTimeInterval)) onFastClick(v) else onSingleClick(v) } /** * 是否是快速点击 * * @param v 点击的控件 * @param interval 时间间期(毫秒) * @return true:是,false:不是 */ private fun isFastDoubleClick(v: View, interval: Long): Boolean { val viewId = v.id val nowTime = System.currentTimeMillis() val timeInterval = abs(nowTime - mLastClickTime) return if (timeInterval < interval && viewId == mLastClickViewId) { // 快速点击事件 true } else { // 单次点击事件 mLastClickTime = nowTime mLastClickViewId = viewId false } } protected open fun onFastClick(v: View?) {} protected abstract fun onSingleClick(v: View?) }
差点忘了... 相应的扩展函数:
fun View?.noShake(block: (v: View?) -> Unit) { this?.apply { setOnClickListener(object : NoShakeClickListener() { override fun onSingleClick(v: View?) { block.invoke(v) } }) } }
inline fun <T : View> T.noShake(crossinline listener: T.() -> Unit) = this.noShake(1000, listener) inline fun <T : View> T.noShake(windowDuration: Long = 500, crossinline listener : T.() -> Unit) = RxView.clicks(this) .throttleFirst(windowDuration, TimeUnit.MILLISECONDS) .subscribe(object : Observer<Any> { override fun onSubscribe(d: Disposable) {} override fun onError(e: Throwable) {} override fun onComplete() {} override fun onNext(o: Any) { listener() } })
1 移除了对View.OnClickListener的依赖, 在不破坏原有代码已设置好的OnClickListener基础上继续处理事件防抖;
2 支持简单处理事件和泛型回调
/** * 事件防抖 * 注: 不仅适用于 View , 其他控件如: MenuItem 同样适用 * * 1.既适用于单个`View`事件防抖, 也适用于`Adapter`中`ItemView`事件防抖 * 2.如果事件为跳转到新的`Activity`, 该`Activity`启动模型应为`android:launchMode="singleTop"` */ open class NoShakeClickListener2 @JvmOverloads constructor(interval: Long = 500L) { private var mTimeInterval = 500L private var mLastClickTime: Long = 0 //最近一次点击的时间 private var mLastClick: Any? = null //最近一次点击的控件 View or MenuItem ... init { mTimeInterval = interval } fun proceedClick() { if (isFastClick(null, mTimeInterval)) onFastClick(null) else onSingleClick(null) } fun <T> proceedClick(item: T?) { if (isFastClick(item, mTimeInterval)) onFastClick(item) else onSingleClick(item) } /** * 是否是快速点击 * * @param item 点击的控件 View or MenuItem ... * @param interval 时间间期(毫秒) * @return true:是,false:不是 */ private fun <T> isFastClick(item: T?, interval: Long): Boolean { val nowTime = System.currentTimeMillis() val timeInterval = abs(nowTime - mLastClickTime) return if (timeInterval < interval && item == mLastClick) { // 快速点击事件 true } else { // 单次点击事件 mLastClickTime = nowTime mLastClick = item false } } protected open fun onFastClick(item: Any?) {} protected open fun onSingleClick(item: Any?) {} }
1 简单使用
//快速点击事件 val fastClick=object :NoShakeClickListener2(){ override fun onFastClick(item: Any?) { super.onFastClick(item) //此时 item == null Log.e("123", "onFastClick Click") } } BottomNavigationView.setOnNavigationItemSelectedListener { switchPage(it.itemId) fastClick.proceedClick() true }
2 回传参数
//快速点击事件 val fastClick=object :NoShakeClickListener2(){ override fun onFastClick(item: Any?) { super.onFastClick(item) //此时 item == null 为 proceedClick(it) 中的 it Log.e("123", "onFastClick Click") } } BottomNavigationView.setOnNavigationItemSelectedListener { switchPage(it.itemId) fastClick.proceedClick(it) true }
感谢你能够认真阅读完这篇文章,希望小编分享的“Android中View事件防抖的处理方案”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注亿速云行业资讯频道,更多相关知识等着你来学习!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。