// 1. this doesn't works
fun View.setDebouncingOnClickListener(onSafeClick: (View) -> Unit) {
setOnClickListener(DebouncingOnClickListener {
onSafeClick(it)
})
}
// 2. this works
fun View.setDebouncingOnClickListener(onSafeClick: (View) -> Unit) {
val listener = DebouncingOnClickListener {
onSafeClick(it)
}
setOnClickListener(listener)
}
“doesn’t work” my onClick method doesn’t overrided by DebouncingClickListener, unless I choose option 2, here’s my code DebouncingClickListener interface (written in java):
Copy code
public interface DebouncingOnClickListener extends View.OnClickListener {
Clicked clicked = new Clicked(0);
void doClick(View v);
@Override
default void onClick(View v) {
if (!clicked.get()) {
clicked.set(SystemClock.elapsedRealtime());
doClick(v);
}
}
}
Copy code
// when I tried to invoke using options 1
viewInKotlin.setDeouncingClickListener {
// pressed twice, will show toast twice
}