Created
July 14, 2020 19:58
-
-
Save houssemzaier/2babd84e9827cc9f8eae7ad40cdc6178 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
package fr.francetv.francetvsport.arch.presentation.base | |
import android.view.View | |
import android.widget.Toast | |
class DebouncingOnClickListener( | |
private val intervalMillis: Long, | |
private val doClick: ((View) -> Unit) | |
) : View.OnClickListener { | |
private var enabled = true | |
override fun onClick(v: View) { | |
if (enabled) { | |
enabled = false | |
v.postDelayed2(intervalMillis) { enabled = true } | |
doClick(v) | |
} | |
} | |
private fun View.postDelayed2(delayMillis: Long, action: () -> Unit) = postDelayed(action, delayMillis) | |
} | |
private fun View.setOnCLick(intervalMillis: Long = 0, doClick: (View) -> Unit) = | |
setOnClickListener(DebouncingOnClickListener(intervalMillis = intervalMillis, doClick = doClick)) | |
fun testDebouncingOnClickListener(v: View) { | |
v.setOnCLick { | |
Toast.makeText(v.context, "No double click any more! ", Toast.LENGTH_SHORT).show() | |
} | |
v.setOnCLick(500) { | |
Toast.makeText(v.context, "No double click any more! ", Toast.LENGTH_SHORT).show() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment