我在使用recycleView时遇到了一些麻烦。 我想使用DiffUtil更新recycleview。 但我不明白为什么它不起作用。 这是我的DiffUtil
public class ConsumableDiffCallBack extends DiffUtil.Callback {
private List<ConsumableByTask> oldListOfConsumableByTask;
private List<ConsumableByTask> newListOfConsumableByTask;
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return oldListOfConsumableByTask.get(oldItemPosition).getQuantity() != newListOfConsumableByTask.get(newItemPosition).getQuantity();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
return oldListOfConsumableByTask.get(oldItemPosition).equals(newListOfConsumableByTask.get(newItemPosition));
}}
我的适配器支架
public class ConsumeHolder extends RecyclerView.ViewHolder {
private Button addConsumeBtn;
public ConsumeHolder(View itemView) {
/**
* add/change data and send it to server.
* after close ConsumablesByTaskActivity.
*/
addConsumeBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//do something
listener.sendDataAndCloseFrame();
}
});}}
适配器类中的更新方法
public void updateList(List<ConsumableByTask> list){
final ConsumableDiffCallBack consumableDiffCallBack = new ConsumableDiffCallBack(this.consumableByTaskList,list);
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(consumableDiffCallBack);
this.consumableByTaskList.clear();
this.consumableByTaskList.addAll(list);
diffResult.dispatchUpdatesTo(this);
}
活动方法
public void sendDataAndCloseFrame() {
List<ConsumableByTask> listOfConsumable = //get new list from server;
adapter.updateList(listOfConsumable);
finish();
}
感谢。
答案 0 :(得分:0)
我在我的RecyclerAdapater中使用它
private List<bindableAssignment> dataBaseItemList;
public void setItems(final List<bindableAssignment> argumentList) {
if (dataBaseItemList == null) {
dataBaseItemList = argumentList;
notifyItemRangeChanged(0, dataBaseItemList.size());
} else {
DiffUtil.DiffResult result = DiffUtil.calculateDiff(new DiffUtil.Callback() {
@Override
public int getOldListSize() {
return dataBaseItemList.size();
}
@Override
public int getNewListSize() {
return argumentList.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
return dataBaseItemList.get(oldItemPosition).getId() == argumentList.get(newItemPosition).getId();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
bindableAssignment oldItem = dataBaseItemList.get(oldItemPosition);
bindableAssignment newItem = argumentList.get(newItemPosition);
// Change this to apply your object
return oldItem.getId() == newItem.getId()
&& oldItem.getName().equals(newItem.getName())
&& (oldItem.getDesc() != null && newItem.getDesc() != null) && oldItem.getDesc().equals(newItem.getDesc())
&& oldItem.getClassId() == newItem.getClassId()
&& oldItem.getDueTime() == newItem.getDueTime();
}
});
dataBaseItemList = argumentList;
result.dispatchUpdatesTo(this);
}
Log.d("dataBindingAdapter", "Changing data with " + dataBaseItemList.size() + " elements");
}
希望它适合你。