gpt4 book ai didi

android - 如何使用 DiffUtils 更新 RecyclerView 中的列表?

转载 作者:行者123 更新时间:2023-12-03 16:50:40 25 4
gpt4 key购买 nike

使用 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

最佳答案

您可以在 youtube 中创建一个模板,就像此视频中所示:

https://www.youtube.com/watch?v=y31fzLe2Ajw

这是适配器的示例。

public class CartFragAdapter extends RecyclerView.Adapter<CartFragAdapter.CartFragViewHolder> {

private static final String TAG = "debinf PurchaseAdap";

private static final DiffUtil.ItemCallback<ProductsObject> DIFF_CALLBACK = new DiffUtil.ItemCallback<ProductsObject>() {
@Override
public boolean areItemsTheSame(@NonNull ProductsObject oldProduct, @NonNull ProductsObject newProduct) {
Log.i(TAG, "areItemsTheSame: old is "+oldProduct.getCode()+" ; new is "+newProduct.getCode());
return oldProduct.getCode().equals(newProduct.getCode());
}

@Override
public boolean areContentsTheSame(@NonNull ProductsObject oldProduct, @NonNull ProductsObject newProduct) {
Log.i(TAG, "areContentsTheSame: old is "+oldProduct.getPrice()+" ; new is "+newProduct.getPrice());
return oldProduct.getPrice() == newProduct.getPrice();
}
};

private AsyncListDiffer<ProductsObject> differ = new AsyncListDiffer<ProductsObject>(this, DIFF_CALLBACK);

@NonNull
@Override
public CartFragViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_purchase, parent, false);
return new CartFragViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull CartFragViewHolder holder, int position) {
final ProductsObject purchaseList = differ.getCurrentList().get(position);

holder.mCode.setText(purchaseList.getCode());
holder.mPrice.setText(String.valueOf(purchaseList.getPrice()));
holder.mDescription.setText(purchaseList.getDescription());


}

@Override
public int getItemCount() {
Log.i(TAG, "getItemCount");
return differ.getCurrentList().size();
}

public void submitList(List<ProductsObject> products){
Log.i(TAG, "submitList: products.size is "+products.size());
differ.submitList(products);
}

public class CartFragViewHolder extends RecyclerView.ViewHolder {

public TextView mCode, mPrice, mDescription;

public CartFragViewHolder(@NonNull View itemView) {
super(itemView);

mCode = (TextView) itemView.findViewById(R.id.item_productCode);
mPrice = (TextView) itemView.findViewById(R.id.item_productPrice);
mDescription = (TextView) itemView.findViewById(R.id.item_productDescription);
}
}
}

主要 Activity 你这样称呼适配器:
CartFragAdapter adapter = new CartFragAdapter();
adapter.submitList(inputData);

我希望它有帮助!

关于android - 如何使用 DiffUtils 更新 RecyclerView 中的列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57950204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com