Advertisement
Guest User

Untitled

a guest
Sep 7th, 2020
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.66 KB | None | 0 0
  1.  
  2. fun View.setIntervalOnClick(intervalMillis: Long = 0, doClick: (View) -> Unit) =
  3.         setOnClickListener(DebouncingOnClickListener(intervalMillis, doClick))
  4.  
  5.  
  6.  
  7. class DebouncingOnClickListener(
  8.         private val intervalMillis: Long,
  9.         private val doClick: ((View) -> Unit)
  10. ) : View.OnClickListener {
  11.  
  12.     override fun onClick(v: View) {
  13.         if (enabled) {
  14.             enabled = false
  15.             v.postDelayed(ENABLE_AGAIN, intervalMillis)
  16.             doClick(v)
  17.         }
  18.     }
  19.  
  20.     companion object {
  21.         @JvmStatic
  22.         var enabled = true
  23.         private val ENABLE_AGAIN =
  24.                 Runnable { enabled = true }
  25.     }
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement