【问题标题】:What is notifyItemRangeChanged(0, this.data.size()); in this example and how does it work?什么是 notifyItemRangeChanged(0, this.data.size());在这个例子中,它是如何工作的?
【发布时间】:2016-04-04 02:51:42
【问题描述】:

我了解ViewHolderonBindViewHolder 是如何工作的,但是我不清楚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;
        }
    }
}

【问题讨论】:

  • notifyItemRangeChanged 是您在适配器中的自定义方法,您需要在您设置适配器的 Mainclass 中调用它。
  • 你检查过文档吗? :http://developer.android.com/intl/zh-cn/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyItemRangeChanged(int, int)
  • 格式化的代码和描述,希望有更好的可读性(保持所有代码不变,但 OP 可能会考虑将其缩减为只需要的部分,而不是整个代码转储)。
  • 如果这有帮助,请接受答案,以便其他人也更容易

标签: java android android-recyclerview notifydatasetchanged


【解决方案1】:

RecyclerView中要设置的数据发生变化时,Adapter需要得到数据变化的通知,这样它才能改变中的数据>recyclerview。

方法

notifyItemRangedChanged(fromIndex,toIndex);

用于通知适配器在整个数据中某些数据集发生了变化,并告诉适配器适配器应该刷新数据并将其重新加载到recyclerView中,从fromIndex到toIndex作为传递给方法。

如果您有多个数据更改但不是全部,请使用此方法,这些更改的数据也在集群中,因此您可以说从第 5 到第 10 个索引数据发生了更改。

如果所有数据都改变了调用:

notifyDataSetChanged();

如果只有一个数据项被更改,则调用:

notifyItemChanged(dataPosition);

【讨论】:

  • 这里似乎有些不对劲。 notifyItemRangedChanged 的​​第二个参数是 itemCount。它不是 toIndex。函数 notifyItemRangeChanged(int positionStart, int itemCount) 通知任何已注册的观察者,从 positionStart 位置开始的 itemCount 项已更改。
  • 查看这里的工作示例:stackoverflow.com/a/38796098/3904109
【解决方案2】:

使用 notifyItemRangeChanged(0, this.data.size()) 是不好的做法。 最好的方法是使用带有有效负载的 notifyItemChanged 或 notifyItemRangeChanged。

Payload - 可选参数(键)。这让您有机会检查您需要什么样的更新。

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

在此示例中,有效负载用于检查适配器何时应仅更新文本。

【讨论】:

    【解决方案3】:

    在您的情况下,您没有这样做 (notifyItemRangeChanged),因为您可以调用 notifyDataSetChanged();,因为您告诉适配器整个列表已更改,而不是特定位置。

    【讨论】:

      猜你喜欢
      • 2012-11-06
      • 2021-10-05
      • 2017-11-26
      • 2021-10-14
      • 2021-10-05
      • 1970-01-01
      • 1970-01-01
      • 2014-10-22
      • 2018-08-04
      相关资源
      最近更新 更多