Implement double click function using RxBinding

Ariel Silva
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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Ariel Silva
Ariel Silva

Written by Ariel Silva

Tech Lead | Product Design | Digital Innovation

No responses yet

Write a response