Created
August 22, 2018 17:18
-
-
Save hilfritz/4384576b9aba5ad02dbfaa62c938f24b to your computer and use it in GitHub Desktop.
Android + RxJava: Listen to view clicks
This file contains hidden or 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
//https://stackoverflow.com/questions/25457737/how-to-create-an-observable-from-onclick-event-android | |
Observable<View> clickEventObservable = Observable.create(new Observable.OnSubscribe<View>() { | |
@Override | |
public void call(final Subscriber<? super View> subscriber) { | |
viewIWantToMonitorForClickEvents.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
if (subscriber.isUnsubscribed()) return; | |
subscriber.onNext(v); | |
} | |
}); | |
} | |
}); | |
// You can then apply all sorts of operation here | |
//you can add throttle to prevent rapid button clicks like so: | |
// clickEventObservable.throttleFirst(600, TimeUnit.MILLISECONDS) | |
Subscription subscription = clickEventObservable.flatMap(/* */); | |
// Unsubscribe when you're done with it | |
subscription.unsubscribe(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment