我理解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中设置的数据发生更改时,适配器需要收到有关数据更改的通知,以便它可以更改recyclervIEw中的数据.
方法
notifyItemRangedChanged(fromIndex,toIndex);
用于通知适配器在整个数据中更改了一组数据,它告诉适配器适配器应刷新数据并将其重新加载到从传入方法的fromIndex到toIndex的recyclerVIEw中.
如果您有多个数据已更改但不是全部,则使用此方法,这些已更改的数据也在群集中,以便您可以说从第5到第10个索引数据已更改.
如果更改了所有数据,请致电:
notifyDataSetChanged();
如果只更改了一个dataItem,则调用:
notifyItemChanged(dataposition);
总结 以上是内存溢出为你收集整理的java – 什么是notifyItemRangeChanged(0,this.data.size());在这个例子中,它是如何工作的?全部内容,希望文章能够帮你解决java – 什么是notifyItemRangeChanged(0,this.data.size());在这个例子中,它是如何工作的?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)