Forked from wutianlong/gist:588f8061579cd33461078919912f1685
Created
August 8, 2016 03:45
-
-
Save pyadav/efd1c4f16a52a5f280809864d656fa81 to your computer and use it in GitHub Desktop.
DoubleClick----RxJava
This file contains 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
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"); | |
} | |
}); | |
} |
I think observeOn
is not necessary.
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
nice, very useful, thanks! I would add a filter and a map to get the first click (almost similar to the 2nd) and put everything inside a ObservableTransformer to use with compose: