8

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

4

3 回答 3

7

您可以在 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);
        }
    }
}

MainActivity你这样调用适配器:

CartFragAdapter adapter = new CartFragAdapter();
adapter.submitList(inputData);

我希望它有帮助!

于 2019-10-12T21:02:59.980 回答
0

观察这段代码

这是我的 diffutil 回调

public class MyDiffUtilCallback extends DiffUtil.Callback {

List<String> oldlist;
List<String > newlist;

public MyDiffUtilCallback(List<String> oldlist, List<String> newlist) {
    this.oldlist = oldlist;
    this.newlist = newlist;
}
@Override
public int getOldListSize() {
    return oldlist.size();
}
@Override
public int getNewListSize() {
    return newlist.size();
}
@Override
public boolean areItemsTheSame(int olditempostion, int newitemPostion) {
    return olditempostion==newitemPostion;
}
@Override
public boolean areContentsTheSame(int olditempostion, int newitemPostion) {
    return olditempostion==newitemPostion;
}
@Nullable
@Override
public Object getChangePayload(int oldItemPosition, int newItemPosition) {
    return super.getChangePayload(oldItemPosition, newItemPosition);
}

}

这是使用 diffutilcallback 的 recyclerview adpater

public class recyclerviewAdapter extends RecyclerView.Adapter<recyclerviewAdapter.Viewholder> {

List<String> datasource;

public recyclerviewAdapter(List<String> datasource) {
    this.datasource = datasource;
}

@Override
public Viewholder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.demorow,viewGroup,false);
    return new Viewholder(itemView);
}

@Override
public void onBindViewHolder(@NonNull Viewholder viewholder, int i) {
    viewholder.tv_demo_data_row.setText(datasource.get(i));
}

@Override
public int getItemCount() {
    return datasource.size();
}

public static class Viewholder extends RecyclerView.ViewHolder {

    TextView tv_demo_data_row;

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

        tv_demo_data_row=itemView.findViewById(R.id.tv_demo_data_row);
    }
}

//DIFF CALLBACK STATE
public void insertdata(List<String> insertList){
    /**
     * Insert list insert data to list
     */
    MyDiffUtilCallback diffUtilCallback = new MyDiffUtilCallback(datasource,insertList);
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffUtilCallback);
    datasource.addAll(insertList);
    diffResult.dispatchUpdatesTo(this);
}

public void updateList(List<String> newList){
    /**
     * update list clear old data and update new data
     */
    MyDiffUtilCallback diffUtilCallback = new MyDiffUtilCallback(datasource,newList);
    DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(diffUtilCallback);
    datasource.clear();
    datasource.addAll(newList);
    diffResult.dispatchUpdatesTo(this);
}}

在这里我可以更新按钮点击事件的活动列表

insert_data.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //inserting the data
            List<String> insertlist = new ArrayList<>(); //asign old list
            for(int i=0;i<2;i++){
                insertlist.add(UUID.randomUUID().toString()); // insert new list
                adapter.insertdata(insertlist);
                faster_recyclerview.smoothScrollToPosition(adapter.getItemCount()-1); //auto scroll to last postion
            }
        }
    });

用您的模型类替换字符串列表

于 2019-09-16T05:15:10.153 回答
0

并且除了以上(使用DiffUtils)之外,如果你使用Clicks来改变RecyclerView的内容,使

ViewHolder implements View.OnClickListener

并使用它在 onBindViewHolder 中设置 OnClickListener

.setOnClickListener(holder);

并通过

this.getAdapterPosition()
private class HistoryRecyclerAdapter extends RecyclerView.Adapter<HistoryRecyclerAdapter.ViewHolder> {
        private List<HistoryEntry> entries;

        HistoryRecyclerAdapter(List<HistoryEntry> entries) {
            this.entries = entries;
        }

        public void updateContainer(List<HistoryEntry> list){
            DiffUtil.DiffResult diffResult = DiffUtil.calculateDiff(new DiffUtil.Callback() {
                @Override
                public int getOldListSize() { return entries.size(); }

                @Override
                public int getNewListSize() { return list.size(); }

                @Override
                public boolean areItemsTheSame(int oldItemPosition, int newItemPosition) {
                    return entries.get(oldItemPosition).getId()==list.get(newItemPosition).getId();
                }

                @Override
                public boolean areContentsTheSame(int oldItemPosition, int newItemPosition) {
                    HistoryEntry old = entries.get(oldItemPosition);
                    HistoryEntry neww = list.get(newItemPosition);
                    return  old.getExpression().equals(neww.getExpression())
                            && old.getResult().equals(neww.getResult());
                }
            }, false);//detectMoves=true потом позволит вызвать анимацию если строка просто была перемещена
            entries = list;
            diffResult.dispatchUpdatesTo(this);
        }

        class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
            //some Views
            final Button delBtn;

            ViewHolder(View view){
                super(view);
                //some Views
                delBtn = (Button) view.findViewById(R.id.delete_btn);
            }

            @Override
            public void onClick(View v) {
                switch (v.getId()){
                    //some cases
                    case R.id.delete_btn:
                        historyManager.delete(entries.get(this.getAdapterPosition()));
                        break;
                }
            }
        }

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

        @Override
        public void onBindViewHolder(HistoryRecyclerAdapter.ViewHolder holder, final int position) {
            //some code
            holder.delBtn.setOnClickListener(holder);
        }

        @Override
        public int getItemCount() {
            return entries.size();
        }

    }

早期我在 onBindViewHolder 中使用了最终 int 位置并且不明白出了什么问题......)))

于 2020-08-29T02:54:39.847 回答