Last active
January 20, 2021 08:13
-
-
Save mertceyhan/964bdf0f4bc5d9f71d1e62ad8d2fcb92 to your computer and use it in GitHub Desktop.
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
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