- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我了解 ViewHolder
的 onBindViewHolder
是如何工作的,但是我不清楚 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;
}
}
}
最佳答案
当要在RecyclerView中设置的数据发生变化时,Adapter需要得到数据变化的通知,以便它可以改变中的数据>回收站 View 。
方法
notifyItemRangedChanged(fromIndex,toIndex);
用于通知适配器整个数据中的某些数据集发生了变化,它告诉适配器适配器应该刷新数据并将其重新加载到 recyclerView 中,从传入方法的 fromIndex 到 toIndex 开始。
如果您更改了多个数据但不是全部,请使用此方法,这些更改的数据也在集群中,因此您可以说从第 5 个到第 10 个索引数据已更改。
如果所有数据都改变调用:
notifyDataSetChanged();
如果只有一个 dataItem 被更改,则调用:
notifyItemChanged(dataPosition);
关于java - 什么是 notifyItemRangeChanged(0, this.data.size());在这个例子中,它是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36393881/
我遇到了与 RecyclerView#Adapter 有关 notifyItemRangeChanged 的问题。似乎如果 Adapter 从上次调用 getItemCount 时认为它的大小为 0,
我有一个数据集,它可能会在刷新时发生变化。很难确定现有数据集中的哪些条目已更改。我通读了 recyclerview 适配器说明,我的主要问题是。 确定更改了哪个数据 View ,然后对受影响的范围使用
布局设计: HorizontalScrollView-parent, holding RV[ RecyclerView=>(GridLayoutManager) (orientation ve
精简版 我调用了 adapter.notifyItemRangeChanged(),但看到(通过日志语句)指定范围之外的位置正在重新绑定(bind)。什么可能导致这种情况? 更长的版本 我的应用程序包
我了解 ViewHolder 的 onBindViewHolder 是如何工作的,但是我不清楚 notifyItemRangeChanged(0, this.data.size()); 在此示例中的工
我是一名优秀的程序员,十分优秀!