Last active
August 7, 2023 13:18
-
-
Save tpakis/d733fdaa2982be7d806e2807ec4eb21c to your computer and use it in GitHub Desktop.
ClickListener with debounce
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DebouncingOnClickListener( | |
private val intervalMillis: Long, | |
private val doClick: ((View) -> Unit) | |
) : View.OnClickListener { | |
override fun onClick(v: View) { | |
if (enabled) { | |
enabled = false | |
v.postDelayed(ENABLE_AGAIN, intervalMillis) | |
doClick(v) | |
} | |
} | |
companion object { | |
@JvmStatic | |
var enabled = true | |
private val ENABLE_AGAIN = | |
Runnable { enabled = true } | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment