我正在使用 diff utils 更新我的回收器视图(来自视图模型的实时数据返回一个列表),我有一个项目装饰,它向列表中的最后一个元素添加了大量的填充,但是 diff utils 正确更新了回收器视图通过调用 notifyItemInserted 而不是 notifyDataSetChanged,装饰仅应用于添加的最后一个元素,因此如果我将一个项目添加到我的回收站视图并调用通知数据集更改适配器重建,我的所有项目最后都会有大量的填充所有项目和填充仅在最后一个元素上(这是我想要的),如果我两次添加相同的项目,它会在最后一次添加之前添加它,例如,如果我有一个 1、2、3 的列表并且我添加在现有的 3 之前添加了另外 3 个,所以 1、2、3b、3a,有没有办法对此进行更多控制?
物品装饰
public class PredictionsHorizontalSpaceCardDecoration extends RecyclerView.ItemDecoration {
private final int horizontalSpace;
private final int endSpace;
public PredictionsHorizontalSpaceCardDecoration(int horizontalSpace, int endSpace) {
this.horizontalSpace = horizontalSpace;
this.endSpace = endSpace;
}
@Override
public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
Log.d("Deco", "parent child count " + (parent.getChildCount() - 1) + " position " + parent.getChildAdapterPosition(view));
outRect.right = horizontalSpace;
outRect.top = horizontalSpace;
outRect.bottom = horizontalSpace;
outRect.left = horizontalSpace;
if (parent.getChildAdapterPosition(view) == parent.getChildCount() - 1){
outRect.right = endSpace;
}
}
@Override
public void onDraw(@NonNull Canvas c, @NonNull RecyclerView parent, @NonNull State state) {
super.onDraw(c, parent, state);
}
}
差异工具
public class MyDiffCallback extends DiffUtil.Callback{
List<Card> oldCards;
List<Card> newCards;
String TAG = "diffUtils";
public MyDiffCallback(List<Card> newCards, List<Card> oldCards) {
this.newCards = newCards;
this.oldCards = oldCards;
}
@Override
public int getOldListSize() {
return oldCards.size();
}
@Override
public int getNewListSize() {
return newCards.size();
}
@Override
public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
boolean areItemsTheSame = oldCards.get(oldItemPosition).getCardId() == newCards.get(newItemPosition).getCardId();
Log.d(TAG,"areItemsTheSame " + areItemsTheSame);
return oldCards.get(oldItemPosition).getCardId() == newCards.get(newItemPosition).getCardId();
}
@Override
public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
boolean areItemsTheSame = oldCards.get(oldItemPosition).equals(newCards.get(newItemPosition));
Log.d(TAG,"areContentsTheSame " + areItemsTheSame);
return oldCards.get(oldItemPosition).equals(newCards.get(newItemPosition));
}
@Nullable
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
//you can return particular field for changed item.
Log.d(TAG,"getChangePayload ");
return super.getChangePayload(oldItemPosition, newItemPosition);
// return newCards.get(newItemPosition).getCardId();
}
}
从回收站视图适配器更新
public class CalculateDiffUtils extends AsyncTask<Void, Void, DiffUtil.DiffResult> {
private List<Card> oldCardList;
private List<Card> newCardList;
CalculateDiffUtils(List<Card> oldCardList, List<Card> newCardList) {
this.oldCardList = oldCardList;
this.newCardList = newCardList;
}
@Override
protected DiffUtil.DiffResult doInBackground(Void... params) {
return DiffUtil.calculateDiff(new MyDiffCallback(newCardList, oldCardList));
}
@Override
protected void onPostExecute(DiffUtil.DiffResult diffResult) {
super.onPostExecute(diffResult);
dispatchUpdates(diffResult, newCardList);
}
}
public void dispatchUpdates(DiffUtil.DiffResult diffResult, List<Card> newCardList){
this.cardList.clear();
this.cardList.addAll(newCardList);
diffResult.dispatchUpdatesTo(this);
}