RxAndroid 按钮点击观察者? [英] RxAndroid button click Observer?

查看:29
本文介绍了RxAndroid 按钮点击观察者?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

各位程序员好,

我使用 RxAndroid 在按下按钮时每 3 秒进行一次间隔 API 调用.

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

我的问题是是否有办法使按钮(或它的 onClick 侦听器)也成为 Observable 并将其与其他人链接起来.

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.

类似于 buttonFetchIntervalWeather.subscribe(fetchWeatherInterval);

Something like buttonFetchIntervalWeather.subscribe(fetchWeatherInterval);

推荐答案

使用 RxBinding

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

throttleFirst 只是在接下来的 5 秒内停止进一步的事件,所以如果用户多次点击按钮,在接下来的 5 秒内不会再次触发相同的 fetchWeatherInterval,当然.

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 将一个 observable 的输出转换为另一个 observable,在这种情况下,从点击事件到 fetchWeatherInterval.如果您需要更多信息,请阅读文档.

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.

此外,RxJava2 也可以工作,我只是为 RxJava1 回答了这个问题.只需将订阅更改为一次性即可.

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

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

或者使用 lambda:

Or with lambda:

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

这篇关于RxAndroid 按钮点击观察者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆