Frage

I am writing an android app and using rxjava to handle user input events. Basically what I want to do is, emit when a button is clicked, and then drop subsequent emissions for some period of time afterwards (like a second or two), essentially to prevent having to process multiple clicks of the button.

War es hilfreich?

Andere Tipps

For preventing fast clicks i use this code

RxView.clicks(your_view)
            .throttleFirst(300, TimeUnit.MILLISECONDS)
            .subscribe {
                //on click
            }

Continuing zsxwing's Answer:

If you're not using RxBinding library but only RxJava then,

io.reactivex.Observable.just(view_obj)
                .throttleFirst(1, TimeUnit.SECONDS)// prevent rapid click for 1 seconds
                .blockingSubscribe(o -> {                     
                    startActivity(...);
                });

This can be done with share debounce and buffer operator

Observable<Object> tapEventEmitter = _rxBus.toObserverable().share();
Observable<Object> debouncedEventEmitter = tapEventEmitter.debounce(1, TimeUnit.SECONDS);
Observable<List<Object>> debouncedBufferEmitter = tapEventEmitter.buffer(debouncedEventEmitter);

debouncedBufferEmitter.buffer(debouncedEventEmitter)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(new Action1<List<Object>>() {
      @Override
      public void call(List<Objecenter code heret> taps) {
        _showTapCount(taps.size());
      }
    });
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top