thanks <@UDWQJDERW> for suggestion it look that so...
# android
i
thanks @rnpy for suggestion it look that someone already implemented
DebouncedOnClickListener.java
https://gist.github.com/rfreedman/5573388 - this should be a good start for me
g
Instead of this implementation I would just disable button after click and enable it after delay, no need to have weak hsash map all other state for callbacks, just one postDelay
1
i
Interesting approach, however disabled button would change it’s look and this may look like UI glich
g
You also can just set some flag to view tags and check it before trigger next click
Anyway, this gist implementation looks overcomplicated and not really efficient
i
Good point - I will take a closer look soon
r
we have a kotlin base debounce implementation. private class DeBouncingOnClickListener(private val onClickListener: ((View) -> Unit)? = null, val viewOnClickListener: View.OnClickListener? = null) : View.OnClickListener { override fun onClick(v: View) { if (enabled) { enabled = false v.postDelayed(ENABLE_AGAIN, DEBOUNCE_DELAY_IN_MILLIS) onClickListener?.invoke(v) viewOnClickListener?.onClick(v) } } companion object { var enabled: Boolean = true val ENABLE_AGAIN = { enabled = true } const val DEBOUNCE_DELAY_IN_MILLIS = 250L } } fun View.setDebounceClickListener(onClickListener: ((View) -> Unit)?) { if (onClickListener != null) { val deBouncingOnClickListener = DeBouncingOnClickListener(onClickListener = onClickListener) setOnClickListener(deBouncingOnClickListener) } else { setOnClickListener(null) } }
this would ignore all view clicks using this method for click for debounce duration. if that is not neede for your usecase you can move
enabled
boolean and
ENABLE_AGAIN
method out of companion.