Implement double click function using RxBinding
1 min readOct 3, 2019
Make sure you add the following dependencies:
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'io.reactivex.rxjava2:rxkotlin:2.4.0'
implementation 'com.jakewharton.rxbinding3:rxbinding:3.0.0'
implementation 'com.jakewharton.rxbinding3:rxbinding-core:3.0.0'
Add this extension function:
fun View.doubleClick(onComplete: () -> Unit): Disposable {
val timeout = 400L
val timeUnits = TimeUnit.MILLISECONDS
val observable = this.clicks().share()
return observable.buffer(observable.debounce(timeout, timeUnits))
.subscribeOn(AndroidSchedulers.mainThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
if (it.size == 2) onComplete.invoke()
}
}
It counts the number of clicks in given time e.g. timeout
and timeUnits
when the size is equal to2
it invoke the onComplete function.