using notifyItemRemoved or notifyDataSetChanged with RecyclerView in Android

后端 未结 8 2137
忘掉有多难
忘掉有多难 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 18:07

    Tried

    public void removeItem(int position) {
        this.taskLists.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, getItemCount() - position);
    }
    

    and working like a charm.

    0 讨论(0)
  • 2020-12-04 18:08

    You can use getLayoutPosition() from the RecyclerView.ViewHolder

    getLayoutPosition() provides the exact position of item in the layout and code is

    holder.removeButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //Position for remove
                    int modPosition= holder.getLayoutPosition();
                    //remove item from dataset
                    numbers.remove(modPosition);
                    //remove item from recycler view
                    notifyItemRemoved(modPosition);
                }
            });
    
    0 讨论(0)
提交回复
热议问题