using notifyItemRemoved or notifyDataSetChanged with RecyclerView in Android

后端 未结 8 2271
忘掉有多难
忘掉有多难 2020-12-04 17:28

I am creating a list of cards to display using the RecyclerView, where each card has a button to remove that card from the list.

When i use notifyItemRemoved

8条回答
  •  时光说笑
    2020-12-04 17:58

    my mistake , notifyItemChanged(position) is helpless,the item of position can be removed ,and the item of position+1 is fine,but the items start from position+2,you will get an Exception, please use notifyItemRangeChanged(position,getItemCount()); after notifyItemRemoved(position);

    like this:

    public void removeData(int position) {
        yourdatalist.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position,getItemCount());
    }
    

提交回复
热议问题