Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView notifyItemRemoved(position) not working properly

I have a RecyclerView with its RecyclerView.Adapter and view holder. I am trying to delete an item from list, code as follows inside onClick() on delete button in the ViewHolder

int position = getAdapterPosition();  
if(position > -1)
{
Place place = placeList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, getItemCount());
}

Despite removing the view and doing the animation (list also gets affected), the old view (or lower one) still exist or drawn again. For example, if the list starts with size = 5, then i try to remove index 4, he remove 4, then still draw 5 views.

EDIT

If i remove notifyItemRangeChanged() then it does that bug only if i do the following

1- click on delete

2- click button very quickly that takes me to new view

3- going back to the list where i can delete

4- start deleting, and bug happens. 1 item still remain even though the List size = 0 (getItemCount is called with 0).

If i only call NotifyDataSetChanged(), then it removes item, but view just stays there !!

Any help or suggestion is appreciated.

Thanks.

EDIT complete class LINK

like image 450
M.Baraka Avatar asked Apr 05 '17 12:04

M.Baraka


4 Answers

use below code it will solve your problem.

 holder.deleteImg.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if(list.size()!=0){
                list.remove(position);
                notifyItemRemoved(position);
                notifyItemRangeChanged(position,list.size());
            }

         }
    });
like image 97
Abhishek Kumar Avatar answered Oct 18 '22 00:10

Abhishek Kumar


Try this:

placeList.remove(position);
notifyItemRemoved(position);
like image 45
Ferdous Ahamed Avatar answered Oct 17 '22 22:10

Ferdous Ahamed


I've had the same problem, notifyItemRangeChanged() call didn't help while notifyDataSetChanged() did (though stopped the animation). But I am using the ItemTouchHelper on the RecyclerView (to allow for moving items around), and it became obvious that this class was causing the trouble.

The only difference in my case is that to reproduce this overlap-after-delete bug, user has to long press on an item while deleting it.

After I modified the overriden isLongPressDragEnabled() method of ItemTouchHelper.Callback to return false instead of true, the issue was solved.

like image 25
southerton Avatar answered Oct 18 '22 00:10

southerton


  lastImages.remove(position); (lastImages equals your array list)
  newContentAdapter.notifyDataSetChanged();

it is works. You have to remove it in your array not item. then notify adapter. Thats all

like image 39
Canberk Çakmak Avatar answered Oct 17 '22 23:10

Canberk Çakmak