2016-04-04 28 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要設置的數據發生變化時,適配器需要得到通知的數據變化,以便它可以更改數據在recyclerview。

的方法

notifyItemRangedChanged(fromIndex,toIndex); 

被用於通知一些數據集是整個數據中改變適配器,它告訴該適配器應該刷新數據,並重新將其recyclerView從啓動適配器從傳入到方法的fromIndex到toIndex。

如果您有多個數據已更改但不是全部,那些已更改的數據也位於羣集中,因此您可以說從5日到10日索引數據發生更改。

如果所有的數據被更改電話:

notifyDataSetChanged(); 

如果只有一個DataItem的改變,然後調用:

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

在本示例中,有效負載用於檢查適配器何時應僅更新文本。

相關問題