RxAndroid button click Observer?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-09 13:53:16

问题


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);


来源:https://stackoverflow.com/questions/43559952/rxandroid-button-click-observer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!