Skip to content

Instantly share code, notes, and snippets.

@vamjakuldip
Created October 30, 2021 08:22
Show Gist options
  • Save vamjakuldip/3567ad15532b248669e2dbbd59e5ef08 to your computer and use it in GitHub Desktop.
Save vamjakuldip/3567ad15532b248669e2dbbd59e5ef08 to your computer and use it in GitHub Desktop.
This class is used to prevent twice function issue on click event
class OnSingleClickListener(private val clickListener: View.OnClickListener, private val intervalMs: Long = 1000L) : View.OnClickListener {
private var canClick = AtomicBoolean(true)
override fun onClick(v: View?) {
if (canClick.getAndSet(false)) {
v?.run {
postDelayed({
canClick.set(true)
}, intervalMs)
clickListener.onClick(v)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment