using notifyItemRemoved or notifyDataSetChanged with RecyclerView in Android

后端 未结 8 2247
忘掉有多难
忘掉有多难 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:55

    You should add remove listener in ViewHolder class

     button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    
                       onCancel(getAdapterPosition());
    
                }
            });
    
      private void onCancel(int position) {
            if (position >= issues.size())
                return;
            issues.remove(position);
            notifyItemRemoved(position);
        }
    

提交回复
热议问题