0

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


        });
    }
4

1 回答 1

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();
于 2016-12-02T14:37:12.647 回答