2016-04-04 13 views
1

私はViewHolderonBindViewHolderの仕組みを理解していますが、この例ではnotifyItemRangeChanged(0, this.data.size());がどのように動作するのかはっきりしません。notifyItemRangeChanged(0、this.data.size())とは何ですか;この例ではどのように動作しますか?

このアダプタに提供されるデータは、Json形式です。

アダプターは以下の通りです:

public class AdapterQuestion extends RecyclerView.Adapter<AdapterQuestion.ViewQuestion>{ 

    private LayoutInflater mLayoutInflater; 

    //this is an arrayList of questionData objects 
    private ArrayList<QuestionData> data =new ArrayList<>(); 

    //Created the layoutInflator 
    public AdapterQuestion(Context context){ 
      //get from context 
      mLayoutInflater=LayoutInflater.from(context); 
    } 

    public void setBloglist(ArrayList<QuestionData> data){ 
     this.data =data; 
     notifyItemRangeChanged(0, this.data.size()); 
    } 

    @Override 
    public ViewQuestion onCreateViewHolder(ViewGroup parent, int viewType) { 
     //inflates the customQuestion view or converts it to java code 
     View view= mLayoutInflater.inflate(R.layout.customquestion, null); 

     //We now want to convert the View into a ViewQuestion, view Question takes 
     //a view so we pass the view into view question and then return it. 

     ViewQuestion holder=new ViewQuestion(view); 
     return holder; 
    } 

    //ViewGroup parent and ViewType are not being assigned. 
    @Override 
    public void onBindViewHolder(ViewQuestion holder, int position) { 
     //here we need to bind the data to our view, there is currently no Data! 
     //We need to get the data from our JSON 
     //Parameters is a ViewHolder and a Position 
     //This gives us the current information object from the whole arraylist 
     //data.get(position) data is the arraylist and we are getting the current position or index; 
     //That current obj is of Type QuestionData 

     QuestionData currentObj= data.get(position); 

     //we are accessing the Inflated view, or saved view with holder 
     //holder.answerText is the textView in holder. We are then taking that current object 
     //We are getting the text of the current object and setting it to the AnswerText view 
     holder.answerText.setText(currentObj.getMtext()); 
     holder.answerId.setText(currentObj.getId()); 
     holder.mVotes.setText(currentObj.getVotes()); 
     holder.mLikeButton.setTag(currentObj); 
    } 

    @Override 
    public int getItemCount() { 
     return data.size(); 
    } 

    public class ViewQuestion extends RecyclerView.ViewHolder{ 
     //once we create it once the reclycer view will automatically recycle it 
     private TextView answerText; 
     private TextView answerId; 
     private TextView mVotes; 
     private LikeButton mLikeButton; 

     public ViewQuestion (View itemView){ 
      super(itemView); 
      //here we are finding the views by their ID 
      answerText=(TextView)itemView.findViewById(R.id.answerText); 
      answerId=(TextView)itemView.findViewById(R.id.answerId); 
      mVotes=(TextView)itemView.findViewById(R.id.VoteTextView); 
      mLikeButton= (LikeButton)itemView.findViewById(R.id.heart_buttons); 

      mLikeButton.setOnLikeListener(new OnLikeListener() { 

       @Override 
       public void liked(LikeButton likeButton) { 
        Voting vote = new Voting(); 
        vote.onUpVote(convertToString(), 
          getAdapterPosition(),ViewQuestion.this); 
        System.out.print("Adapter Position"+getAdapterPosition()); 
       } 

       @Override 
       public void unLiked(LikeButton likeButton) { 
        Voting onDown=new Voting(); 
        onDown.onDownVote(convertToString(), 
          getAdapterPosition(), ViewQuestion.this); 

       } 
      }); 
     } 

     public String getVoteView(){ 
      String voteView=mVotes.getText().toString(); 
      return voteView; 
     } 

     public String convertToString(){ 
      String converted=answerId.getText().toString(); 
      return converted; 
     } 

     public int convertToInt(){ 
      String converted=answerId.getText().toString(); 
      int ConvertedInt=Integer.parseInt(converted); 
      return ConvertedInt; 
     } 
    } 
} 
+0

'notifyItemRangeChanged'は、あなたのアダプターのカスタムメソッドです。あなたはsetAdapterのMainclassでそれを呼び出す必要があります。 – Kathi

+0

あなたは文書をチェックしましたか? : 'http://developer.android.com/intl/zh-cn/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyItemRangeChanged(int、int)' – Yazan

+0

フォーマットコードと説明、うまくいっています。読みやすさ(すべてのコードをそのまま保ちますが、OPはコードダンプ全体ではなく、必要な部分だけに減らすことを検討したいかもしれません)。 –

答えて

3

RecyclerViewに設定されるデータは、それがデータを変更できるように、データ変更の通知を取得する必要がアダプタを変更した場合in recyclerview。

方法

notifyItemRangedChanged(fromIndex,toIndex); 

は、データのいくつかのセットが全データのうち変更されたアダプタを通知するために使用し、それは、アダプタがデータを更新してから開始recyclerViewに再ロードする必要があり、アダプタに指示されていますメソッドに渡されるfromIndexからtoIndex

複数のデータが変更されているがすべてではない場合、変更されたデータもクラスタ内にあるため、5番目から10番目のインデックスデータが変更されたと言うことができます。

すべてのデータがコールに変更された場合のみ:1データ項目が変更された場合、その後呼び出し

notifyDataSetChanged(); 

を:あなたのケースで

notifyItemChanged(dataPosition); 
0

をあなたはそれをやっていません(notifyItemRangeChanged)右のあなたにかもしれませんがよくnotifyDataSetChanged();に電話することができます。なぜなら、リスト全体が変更されており、特定の位置ではないことをアダプタに伝えているからです。

1

notifyItemRangeChanged(0、this.data.size())を使用するのは悪い習慣です。 最良の方法は、ペイロードでnotifyItemChangedまたはnotifyItemRangeChangedを使用することです。

ペイロード - オプションのパラメータ(キー)。これにより、どのようなアップデートが必要かを確認する機会が与えられます。

public void onBindViewHolder(/*...*/, List payloads) { 
    if (payloads.isEmpty()) { 
     setText(holder, position); 
     downloadBitmap(holder, position); 
    } else if (payloads.contains(SET_ONLY_TEXT)){ 
     setText(holder, position); 
    } 
} 

この例では、アダプタがテキストのみを更新する必要があるかどうかをチェックするためのペイロードです。

関連する問題