Skip to content

Instantly share code, notes, and snippets.

@tpakis
Last active August 7, 2023 13:18
Show Gist options
  • Save tpakis/d733fdaa2982be7d806e2807ec4eb21c to your computer and use it in GitHub Desktop.
Save tpakis/d733fdaa2982be7d806e2807ec4eb21c to your computer and use it in GitHub Desktop.
ClickListener with debounce
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