Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save pyadav/efd1c4f16a52a5f280809864d656fa81 to your computer and use it in GitHub Desktop.
Save pyadav/efd1c4f16a52a5f280809864d656fa81 to your computer and use it in GitHub Desktop.
DoubleClick----RxJava
public void doubleClickDetect(View view){
Observable<Void> observable = RxView.clicks(view).share();
observable.buffer(observable.debounce(200, TimeUnit.MILLISECONDS))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Action1<List<Void>>() {
@Override
public void call(List<Void> voids) {
if(voids.size() >= 2){
//double click detected
}
}
}, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
Timber.e(throwable, "error");
}
});
}
@jamolkhon
Copy link

jamolkhon commented May 23, 2018

I think observeOn is not necessary.

@qqnp1100
Copy link

without RxView

    private void doubleClickDetect(View view) {
        PublishSubject<Integer> publishSubject = PublishSubject.create();
        publishSubject
                .buffer(publishSubject.debounce(200, TimeUnit.MILLISECONDS))
                .filter(list -> list.size() > 1)
                .subscribe(list -> {
                    //double click detected
                });
        view.setOnClickListener(v -> {
            publishSubject.onNext(0);
        });
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment