-->

RxAndroid button click Observer?

2020-06-28 16:32发布

问题:

Hi fellow programmers,

I am using RxAndroid to make an interval API call every 3 seconds when a button is pressed.

private final CompositeDisposable disposables = new CompositeDisposable();

Observable fetchWeatherInterval = Observable.interval(3, TimeUnit.SECONDS)
        .map(new Function<Long, String>() {
            @Override
            public String apply(Long aLong) throws Exception {
                return getWeather("http://samples.openweathermap.org/data/2.5/weather?", "London,uk", "b1b15e88fa797225412429c1c50c122a1");
            }
        })
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread());

Observer displayWeatherInterval = new Observer<String>() {

    @Override
    public void onError(Throwable e) {
        Log.e("Throwable ERROR", e.getMessage());
    }

    @Override
    public void onComplete() {
    }

    @Override
    public void onSubscribe(Disposable d) {
        disposables.add(d);
    }

    @Override
    public void onNext(String value) {
        textViewWeatherInterval.append(value);
    }
};

buttonFetchIntervalWeather.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            fetchWeatherInterval.subscribe(displayWeatherInterval);
        }
    });

My question is if there is a way to make the button (or it's onClick listener) an Observable too and chain it with the others.

Something like buttonFetchIntervalWeather.subscribe(fetchWeatherInterval);

回答1:

Use RxBinding

Subscription s = RxView.clicks(button)
        .throttleFirst(5, TimeUnit.SECONDS) // maybe you want to ignore multiple clicks
        .flatMap(foo -> fetchWeatherInterval)
        .subscribe(displayWeatherInterval);

throttleFirst just stops further events for next 5 seconds so if user clicks the button multiple times, the same fetchWeatherInterval won't be triggered again, for the next 5 seconds, of course.

flatMap converts output of one observable into another observable, in this case from click event to fetchWeatherInterval. Read the docs if you need more info.

Also, RxJava2 works as well, I just answered this for RxJava1. Just change Subscription to Disposable.

Using Observable.create():

Observable.create(new Action1<Emitter<View>>() {
    @Override
    public void call(Emitter<View> emitter) {
        emitter.setCancellation(new Cancellable() {
            @Override
            public void cancel() throws Exception {
                button.setOnClickListener(null);
                emitter.onCompleted();
            }
        });
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                emitter.onNext(v);
            }
        });
    }
}, Emitter.BackpressureMode.DROP);

Or with lambda:

Observable.create(emitter -> {
    emitter.setCancellation(() -> {
        button.setOnClickListener(null);
        emitter.onCompleted();
    });
    button.setOnClickListener(emitter::onNext);
}, Emitter.BackpressureMode.DROP);