RxSearchView同时观察textChanges和Events

时间:2016-12-02 13:47:06

标签: android rx-java searchview rx-binding

我正在使用RxSearchView以“您键入时搜索”的形式查询文本更改

RxSearchView.queryTextChanges(searchView)

但我想在用户提交搜索时抓住,所以我必须使用

RxSearchView.queryTextChangeEvents(searchView)searchView.setOnQueryTextListener

当我使用其中任何一个时,看起来他们正在取消第一个RxSearchView.queryTextChanges,看起来只有1个可观察的附加到SearchView。

如何同时观察这两个事件?

这是完整的代码

private void setupSearch() {
        RxSearchView.queryTextChangeEvents(searchView)
                .skip(1)
                .throttleLast(100, TimeUnit.MILLISECONDS)
                .debounce(200, TimeUnit.MILLISECONDS)
                .onBackpressureLatest()
                .observeOn(AndroidSchedulers.mainThread())
                .filter(new Func1<SearchViewQueryTextEvent, Boolean>() {
                    @Override
                    public Boolean call(SearchViewQueryTextEvent searchViewQueryTextEvent) {
                        final boolean empty = TextUtils.isEmpty(searchViewQueryTextEvent.queryText());
                        if (empty) {
                            //Dont show anything  clear adapter
                        }
                        return !empty;
                    }
                }).subscribe(new Subscriber<SearchViewQueryTextEvent>() {

            @Override
            public void onNext(SearchViewQueryTextEvent searchViewQueryTextEvent) {
                String searchTerm = searchViewQueryTextEvent.queryText().toString();
                if (searchViewQueryTextEvent.isSubmitted()) {
                    submitFullSearch(searchTerm);
                } else {
                    submitRecommendationsSearch(searchTerm);
                }
            }

            @Override
            public void onCompleted() {
            }

            @Override
            public void onError(Throwable e) {
            }


        });
    }

1 个答案:

答案 0 :(得分:1)

由于它覆盖了视图的侦听器,因此只有一个可观察对象,但您可以使用RxSearchView.queryTextChangeEvents(searchView)来监视这两种类型的事件。它提供了SearchViewQueryTextEvent个事件流。对于每个活动,您可以查看isSubmitted()以确定它是提交还是更改事件,并使用queryText()获取当前文字。

以下是如何使用ConnectableObservable将事件分成两个流来分别过滤 -

private void setupSearch() {
    ConnectableObservable<SearchViewQueryTextEvent>  searchObs = RxSearchView.queryTextChangeEvents(searchView).publish();
    searchObs.skip(1)
            .throttleLast(100, TimeUnit.MILLISECONDS)
            .debounce(200, TimeUnit.MILLISECONDS)
            .onBackpressureLatest()
            .observeOn(AndroidSchedulers.mainThread())
            .filter(new Func1<SearchViewQueryTextEvent, Boolean>() {
                @Override
                public Boolean call(SearchViewQueryTextEvent searchViewQueryTextEvent) {
                    final boolean empty = TextUtils.isEmpty(searchViewQueryTextEvent.queryText());
                    if (empty) {
                        //Dont show anything  clear adapter
                    }
                    return !empty;
                }
            }).subscribe(new Subscriber<SearchViewQueryTextEvent>() {

        @Override
        public void onNext(SearchViewQueryTextEvent searchViewQueryTextEvent) {
            String searchTerm = searchViewQueryTextEvent.queryText().toString();
            if (!searchViewQueryTextEvent.isSubmitted()) {
                submitRecommendationsSearch(searchTerm);
            }
        }

        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }
    });

    searchObs.subscribe(new Subscriber<SearchViewQueryTextEvent>() {
        @Override
        public void onCompleted() {

        }

        @Override
        public void onError(Throwable e) {

        }

        @Override
        public void onNext(SearchViewQueryTextEvent searchViewQueryTextEvent) {
            if (searchViewQueryTextEvent.isSubmitted()) {
                submitFullSearch(searchTerm);
            }
        }
    });

    Subscription searchSub = searchObs.connect();