java json notify_java – 什么是notifyItemRangeChanged(0,this.data.size());在这个例子中,它是如何工作的?...

我理解ViewHolder的onBindViewHolder如何工作,但是我不清楚notifyItemRangeChanged(0,this.data.size())如何;适用于此示例以及它的确切功能.

提供给此适配器的数据采用Json格式.

适配器如下:

public class AdapterQuestion extends RecyclerView.Adapter{

private LayoutInflater mLayoutInflater;

//this is an arrayList of questionData objects

private ArrayList data =new ArrayList<>();

//Created the layoutInflator

public AdapterQuestion(Context context){

//get from context

mLayoutInflater=LayoutInflater.from(context);

}

public void setBloglist(ArrayList 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);

标签:notifydatasetchanged,android,java,android-recyclerview

来源: https://codeday.me/bug/20190727/1555465.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java ,回调是一种常见的编程模式,它允许一个对象将某个方法作为参数传递给另一个对象,在某个特定的时间点或条件下被调用。简单来说,回调就是让一个对象在另一个对象完成某些操作后通知它。 回调在 Java 的应用非常广泛,例如在事件处理、GUI编程、异步编程等方面都有应用。回调可以实现两个对象之间的解耦,让代码更加清晰、简洁、易于维护。 在 Java ,回调通常通过接口来实现。例如,一个对象需要在某个条件满足时通知另一个对象,就可以定义一个接口,其包含一个方法,然后将该接口的实例传递给第一个对象。当条件满足时,第一个对象就可以调用该接口的方法,通知第二个对象。 以下是一个简单的示例代码: ```java public interface Callback { void notify(String message); } public class Caller { public void doSomething(Callback callback) { // 某些操作完成后调用回调方法 callback.notify("操作完成"); } } public class Receiver implements Callback { public void notify(String message) { System.out.println("接收到消息:" + message); } } public class Main { public static void main(String[] args) { Caller caller = new Caller(); Receiver receiver = new Receiver(); caller.doSomething(receiver); } } ``` 在上面的代码,Caller 类完成某些操作后调用回调方法,Receiver 类实现了回调接口 Callback,并在回调方法处理接收到的消息。在 Main 类,创建了 Caller 和 Receiver 的实例,然后将 Receiver 的实例作为参数传递给 Caller 的 doSomething 方法,当操作完成后,Caller 就会调用 Receiver 的 notify 方法,通知它操作已经完成。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值