仅当Fragment在ViewPager中可见时,Fire RxJava事件才会更改

时间:2016-09-02 12:20:29

标签: android android-fragments rx-java rx-android

我在EditText中有一个Activity,用户可以在其中输入和搜索内容。在搜索栏下方,我有一个tabs附加到ViewPager。我为标签中的EditText添加了Fragments的RxJava事件更改侦听器。以下是相关代码:

    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 个答案:

答案 0 :(得分:1)

我认为有两种选择。

使用过滤器:

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