-1

I have a delete button in each item of RecyclerView. It works fine but the view below RecyclerView doesn't follow up.

I try to follow Android RecyclerView addition & removal of items but it still not works

Here my code

    public ViewHolder(View itemView) {
        super(itemView);

        btnDelete = (ImageButton)itemView.findViewById(R.id.detail_delete);
        btnDelete.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        removeAt(getAdapterPosition());
    }

    private void removeAt(int position) {
        scheduleList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, scheduleList.size());
    }
Community
  • 1
  • 1
sgennrw
  • 156
  • 8

2 Answers2

0

I think the problem lies here...

notifyItemRangeChanged(position, scheduleList.size());

you're technically lying to the RecyclerView and the adapter by saying you've removed all items located after the specified position...that's what the last parameter is for...number of items to remove which is incorrect because you are passing in the size of the whole list. This what the last parameter stands for...from the javadocs...

itemCount - Number of items removed from the dataset

Basically, you will need to remove that line in the removeAt method so that you only notify the observers once...

private void removeAt(int position) {
    scheduleList.remove(position);
    notifyItemRemoved(position);
}

if it still doesn't work, then you might be having yet another problem somewhere else

Leo
  • 14,625
  • 2
  • 37
  • 55
0

Add the function notifyDataSetChanged(). I was facing the same problem

@Override
    public void onClick(View v) {
        removeAt(getAdapterPosition());
        adapter.notifyDataSetChanged();  //adapter is your card adapter
    }
Chariston
  • 11
  • 6
  • Thanks! but do you have to type any data into that view? I have a new problem which are that data still remain. – sgennrw Feb 24 '17 at 06:39