2016-11-09 2 views
3

私はRxBindingを使用しており、アイテムを再利用するonBindViewHolderメソッドのRecyclerViewアダプタでサブスクリプションを作成しています。すでにEditTextオブジェクトにサブスクライバを割り当てているかどうかを確認する簡単な方法はありますか?その場合はサブスクリプションを削除しますか?RxJava/RxBinding - サブスクリプションが存在するかどうかを確認します。

私のコードは、私はすでに のEditTextオブジェクトへのユーザを割り当てので、そのサブスクリプションを削除した場合かどうかを確認するために、とにかくあり、この

public void onBindViewHolder(final ItemViewHolder holder, int position) { 

    holder.text.setText(mProvider.get(position).text); 
    Subscription textSub = RxTextView.textChanges(holder.text).subscribe(new Action1<CharSequence>() { 
     @Override 
     public void call(CharSequence charSequence) { 
      ... 
     } 
    }); 
    subscriptions.add(textSub); 
} 

答えて

3

のように見えますか?

あなたはそれをクラスメンバとして保持できます。例えば。側では

Subscription textSub = Subscriptions.unsubscribed(); 

、その後

public void onBindViewHolder(final ItemViewHolder holder, final int position) { 

    holder.text.setText(mProvider.get(position).text); 
    textSub.unsubscribe(); 
    textSub = RxTextView.textChanges(holder.text).subscribe(new Action1<CharSequence>() { 
      @Override 
      public void call(CharSequence charSequence) { 
       ... 
      } 
     }); 
} 
+0

、効率的な単位で、この動的サブスクリプションジャグリングのですか?そして、このきちんとした清潔なソリューションに感謝します。 –

+1

*はこの動的サブスクリプションで効率的なジャグリングです*そうです。どういたしまして – Blackbelt

関連する問題