NotifyItemRangeInserted不会触发视图回收

时间:2018-10-24 23:06:13

标签: android android-recyclerview

我想用一个新的列表初始化我的列表,并使用notifyItemRangeInserted(0,List.size())即时通讯。但这不会触发旧视图被回收。如果我仅使用notifyDataset,它将触发它。事实是notifyDataset的操作成本很高,我需要另一种方法。

谢谢

  public class Adapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements Filterable,CustomAdapter {

    private Context mCtx;
    private RecyclerView recycler;
    private List<Pair<Integer,Object>> List;


    public Adapter(Context mCtx, EmployeeClickCallback, RecyclerView recycler,) {
        this.mCtx = mCtx;
        this.recycler = recycler;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    }

    @Override
    public void onViewRecycled(@NonNull RecyclerView.ViewHolder holder) {
        super.onViewRecycled(holder);
        Log.d("onViewRecycled","recycled");
    }

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

        }
    }

    public void setList(List<Pair<Integer,Object>> list){
        if(List!=null)
            List.clear();
        List= list;
        notifyItemRangeInserted(0, List.size());
    }

}

1 个答案:

答案 0 :(得分:0)

您应该通知该项目已删除:

public void setList(List<Pair<Integer,Object>> list){
    if(mList != null) {
        final int size = mList.size();
        mList.clear();
        notifyItemRangeRemoved(0, size);
    }
    mList.addAll(list);
    notifyItemRangeInserted(0, mList.size());
}