Skip to content

Instantly share code, notes, and snippets.

@mertceyhan
Last active January 20, 2021 08:13
Show Gist options
  • Save mertceyhan/964bdf0f4bc5d9f71d1e62ad8d2fcb92 to your computer and use it in GitHub Desktop.
Save mertceyhan/964bdf0f4bc5d9f71d1e62ad8d2fcb92 to your computer and use it in GitHub Desktop.
abstract class OnOneOffClickListener(private var defaultInterval: Int,
private val onOneClick: (View) -> Unit) : View.OnClickListener {
private var lastTimeClicked: Long = 0
override fun onClick(view: View) {
if ((SystemClock.elapsedRealtime() - lastTimeClicked) >= (defaultInterval)) {
lastTimeClicked = SystemClock.elapsedRealtime()
onOneClick(view)
}
}
companion object {
const val DEFAULT_INTERVAL = 1000
}
}
@JvmOverloads
fun View.setOnOneOffClickListener(defaultInterval: Int = DEFAULT_INTERVAL, onOneClick: (View) -> Unit) {
this.setOnClickListener(object : OnOneOffClickListener(
onOneClick = onOneClick, defaultInterval = defaultInterval) {})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment