2016-09-02 19 views
5

我有一個EditTextActivity中,用戶可以在其中鍵入和搜索內容。在搜索欄下方,我有2 tabs附加到ViewPager。我添加了一個RxJava事件更改偵聽器,用於我在選項卡中使用的2 Fragments中的EditText。下面是相關代碼:Fire RxJava事件只有在ViewPager中可見片段時纔會更改

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

我不明白你到底想做而** setUserVisibleHint **僅在ViewPager對用戶可見時調用。 – Amir

+0

我在工具欄中放置了一個搜索欄,當您輸入任何內容時,您會根據當前選擇哪個標籤在2個類別(人員和圖庫)中看到結果。現在的問題是,無論何時輸入任何內容,都會觸發這兩種類型的搜索請求。我只希望所選標籤的搜索查詢被解僱。 –

+0

嘗試取消訂閱onStop中的訂閱。 – drivfe

回答

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; 
     } 
    } 
相關問題