[android]火 RxJava 事件更改时才 Fragment 是可见的ViewPager

发布时间: 2016/10/10 20:55:29
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我有 EditTextActivity 用户可以 type 和寻找的东西。搜索栏,下面我有 2 tabs 连接到 ViewPager 。我添加了一个 RxJava 事件更改listener的 EditText 在 2 Fragments 包含的选项卡。以下是相关代码︰

    private Subscription subscription;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_people_search, container, false);
        ButterKnife.bind(this, view);
        LogUtil.i(TAG, "onCreateView called");

        if (getActivity() != null) {
            inputSearch = (EditText) getActivity().findViewById(R.id.editText11);
        }

        return view;
    }

    @Override
    public void onResumeFragment() {
        LogUtil.i(TAG, "onResumeFragment called");
        if (subscription == null) {
            doSearch();
        }
    }

    private void doSearch() {
        LogUtil.i(TAG, "doSearch called");
        subscription = RxTextView.textChangeEvents(inputSearch)
                .debounce(400, TimeUnit.MILLISECONDS)
                .map(new Func1<TextViewTextChangeEvent, String>() {
                    @Override
                    public String call(TextViewTextChangeEvent textViewTextChangeEvent) {
                        return textViewTextChangeEvent.text().toString().trim();
                    }
                })
                .switchMap(new Func1<String, Observable<SearchUserData>>() {
                    @Override
                    public Observable<SearchUserData> call(String s) {
                        term = s;
                        if (previousTerm.equalsIgnoreCase(term) && TextUtils.isEmpty(term)) {
                            //do not reset pagination variables
                        } else if (!previousTerm.equalsIgnoreCase(term) && TextUtils.isEmpty(term)) {
                            min = 0;
                            max = 14;
                        } else if (previousTerm.equalsIgnoreCase(term) && !TextUtils.isEmpty(term)) {
                            //do not reset pagination variables
                        } else {
                            min = 0;
                            max = 14;
                        }
                        loadMoreOrNot = true;
                        previousTerm = term;
                        DiscoverAPI discoverAPI = restAdapter.create(DiscoverAPI.class);
                        return discoverAPI.getRxPeopleSearchResult(min, max, s, "user");
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(getRxSearchObserver());
    }

我想的是,当搜索的东西时,查询应该去的后端,只现在选择选项卡,而对于这两个选项卡。怎么可以这样?

解决方法 1:

我认为有两个选项。

使用 filter :

    subscription = RxTextView.textChangeEvents(inputSearch)
            .debounce(400, TimeUnit.MILLISECONDS)
            .filter(textViewTextChangeEvent -> getUserVisibleHint())
            .map(..)
            ...

订阅或取消订阅时 setUserVisibleHint 称为 (正如 @Amir 的评论)︰

    @Override
    public void onResumeFragment() {
        //don't subscribe here
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if (subscription != null) {
            subscription.unsubscribe();
            subscription = null;
        }
        if (isVisibleToUser) {
            doSearch();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();

        if (subscription != null) {
            subscription.unsubscribe();
            subscription = null;
        }
    }
赞助商