您好,登錄后才能下訂單哦!
這篇文章主要介紹了Android中View事件防抖的處理方案,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
侵入式防抖處理(NoShakeClickListener) implements View.OnClickListener
無(wú)侵入式防抖處理(NoShakeClickListener2) not implements View.OnClickListener
1. 既適用于單個(gè)View事件防抖, 也適用于Adapter中ItemView事件防抖
2. 如果事件為跳轉(zhuǎn)到新的Activity, 該Activity啟動(dòng)模型應(yīng)為android:launchMode="singleTop"
public abstract class NoShakeClickListener implements View.OnClickListener { private long mTimeInterval = 500L; /** * 最近一次點(diǎn)擊的時(shí)間 */ private long mLastClickTime; /** * 最近一次點(diǎn)擊的控件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); } } /** * 是否是快速點(diǎn)擊 * * @param v 點(diǎn)擊的控件 * @param interval 時(shí)間間期(毫秒) * @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) { // 快速點(diǎn)擊事件 return true; } else { // 單次點(diǎn)擊事件 mLastClickTime = nowTime; mLastClickViewId = viewId; return false; } } protected void onFastClick(View v) {} protected abstract void onSingleClick(View v); }
可簡(jiǎn)寫(xiě)為
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; // 快速點(diǎn)擊事件 } else { mLastClickTime = nowTime; return false; // 單次點(diǎn)擊事件 } } @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 //最近一次點(diǎn)擊的時(shí)間 private var mLastClickViewId = 0 //最近一次點(diǎn)擊的控件ID init { mTimeInterval = interval } override fun onClick(v: View) { if (isFastDoubleClick(v, mTimeInterval)) onFastClick(v) else onSingleClick(v) } /** * 是否是快速點(diǎn)擊 * * @param v 點(diǎn)擊的控件 * @param interval 時(shí)間間期(毫秒) * @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) { // 快速點(diǎn)擊事件 true } else { // 單次點(diǎn)擊事件 mLastClickTime = nowTime mLastClickViewId = viewId false } } protected open fun onFastClick(v: View?) {} protected abstract fun onSingleClick(v: View?) }
差點(diǎn)忘了... 相應(yīng)的擴(kuò)展函數(shù):
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 移除了對(duì)View.OnClickListener的依賴(lài), 在不破壞原有代碼已設(shè)置好的OnClickListener基礎(chǔ)上繼續(xù)處理事件防抖;
2 支持簡(jiǎn)單處理事件和泛型回調(diào)
/** * 事件防抖 * 注: 不僅適用于 View , 其他控件如: MenuItem 同樣適用 * * 1.既適用于單個(gè)`View`事件防抖, 也適用于`Adapter`中`ItemView`事件防抖 * 2.如果事件為跳轉(zhuǎn)到新的`Activity`, 該`Activity`啟動(dòng)模型應(yīng)為`android:launchMode="singleTop"` */ open class NoShakeClickListener2 @JvmOverloads constructor(interval: Long = 500L) { private var mTimeInterval = 500L private var mLastClickTime: Long = 0 //最近一次點(diǎn)擊的時(shí)間 private var mLastClick: Any? = null //最近一次點(diǎn)擊的控件 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) } /** * 是否是快速點(diǎn)擊 * * @param item 點(diǎn)擊的控件 View or MenuItem ... * @param interval 時(shí)間間期(毫秒) * @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) { // 快速點(diǎn)擊事件 true } else { // 單次點(diǎn)擊事件 mLastClickTime = nowTime mLastClick = item false } } protected open fun onFastClick(item: Any?) {} protected open fun onSingleClick(item: Any?) {} }
1 簡(jiǎn)單使用
//快速點(diǎn)擊事件 val fastClick=object :NoShakeClickListener2(){ override fun onFastClick(item: Any?) { super.onFastClick(item) //此時(shí) item == null Log.e("123", "onFastClick Click") } } BottomNavigationView.setOnNavigationItemSelectedListener { switchPage(it.itemId) fastClick.proceedClick() true }
2 回傳參數(shù)
//快速點(diǎn)擊事件 val fastClick=object :NoShakeClickListener2(){ override fun onFastClick(item: Any?) { super.onFastClick(item) //此時(shí) item == null 為 proceedClick(it) 中的 it Log.e("123", "onFastClick Click") } } BottomNavigationView.setOnNavigationItemSelectedListener { switchPage(it.itemId) fastClick.proceedClick(it) true }
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Android中View事件防抖的處理方案”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。