使用 DiffUtil ItemCallback 时,如何使用 DiffCallback 在 RecyclerView 中加载 newList。
当用户选择我希望 RecyclerView 更新的不同尺寸时,我想为用户提供从数据库返回不同尺寸列表的选项。
RecyclerViewAdatper
RecyclerViewAdapter extends ListAdapter<WordEntity, RecyclerViewAdapter.ViewHolder> {
private RecyclerViewAdapter() {
super(DIFF_CALLBACK);
}
private static final DiffUtil.ItemCallback<WordEntity> DIFF_CALLBACK = new DiffUtil.ItemCallback<WordEntiti>() {
@Override
public boolean areItemsTheSame...
@Override
public boolean areContentsTheSame...
};
@Override
public viewHolder onCreateViewHolder...
@Override
public void onVindViewHolder ...
class ViewHolder extends RecyclerView.ViewHolder ...
public void updateWordList(List<WordEntity> words) {
final WordDiffCallBack diffCallBack = new WordDiffCallBack(list???, words);
final DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffCallBack);
this.list???.clear();
this.addAll(words);
diffResult.dispatcheUpdatesTo(this);
}
WordsDiffCallBack
private final List<WordEntity> mOldList;
private final List<WordEntity> mNewList;
public WordsDiffCallBack(List<WordEntity> oldList, List<WordEntity> newList) {
this.mOldList = oldList;
this.mNewList = newList;
}
@Override
public int getOldListSize() {
return mOldList.size();
}
@Override
public int getNewListSize() {
return mNewList.size();
}
@Override
public boolean areItemsTheSame(int OldItemPostion, int newItemPosition) ...
@Override boolean areContentsTheSame(int oldItemPosition, int newItemPosition)...
@Override getChangePayload(int oldItemPosition, int newItemPosition) ...
}
我希望 RecycelView 在用户更改列表大小时自动更新。如何从 ListAdapter 调用旧列表,甚至会更新 RecyclerView