android -recycleview使用notifyItemRemoved无法正常工作

时间:2016-02-29 08:59:04

标签: android android-recyclerview

我正在使用recycleview来填充我的项目。我正在尝试使用notifyItemRemoved从我的recycleview中删除一个项目,但它不会隐藏该行,也不会通知适配器。这是我的适配器代码:

public class CatsAdapter extends RecyclerView.Adapter<CatsAdapter.MyViewHolder> {
    private LayoutInflater inflater;
    private List<CatItems> list;

    public CatsAdapter(Context context, List<CatItems> feedItemList) {
        inflater = LayoutInflater.from(context);
        this.list = feedItemList;
    }


    public void addAll(List<CatItems> catItems) {
        if (this.list == null) {
            this.list = catItems;
        } else {
            this.list.addAll(catItems);
        }
        notifyDataSetChanged();
        notifyItemInserted(list.size());
    }

    public void remove(int pos) {
        this.list.remove(pos);
        notifyItemRemoved(pos);
        notifyItemRangeChanged(pos, getItemCount()-pos);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parrent, int i) {
        View view = inflater.inflate(R.layout.row_grid2, parrent, false);
        MyViewHolder holder = new MyViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(final MyViewHolder viewHolder, final int position) {
        CatItems item = list.get(position);
            viewHolder.del.setTag(item.getId());
            viewHolder.del.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    adapter.remove(position);
                }
            });

    }

    @Override
    public int getItemCount() {
        if (list == null)
            return 0;
        else
            return list.size();
    }


    class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        ImageView img, ten, five,del;

        public MyViewHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(this);
            del = (ImageView) itemView.findViewById(R.id.del);
        }

    }
}

问题出在哪里?

0 个答案:

没有答案