12

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!

EDIT complete class LINK

3
  • I am facing this problem too and tried everything available online but no effect at all. But in my case it does sometimes work properly though while sometimes it remove white space as soon as I touch my list Commented Apr 5, 2017 at 12:23
  • @Vivek Mishra Normal behavior where i click on delete, it works without the notifyItemRangeChanged(). But If i go to new view, go back, >> bug happens
    – M.Baraka
    Commented Apr 5, 2017 at 12:37
  • I called reloaded recycler view inside onSwiped method. It worked! Though it will remove the animation. Commented Aug 26, 2021 at 5:50

10 Answers 10

11

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());
            }

         }
    });
7
  • I did log size before i remove & after, List size is not 0
    – M.Baraka
    Commented Apr 5, 2017 at 12:33
  • You mean you deleted all the rows, still you get one in your list size. Commented Apr 5, 2017 at 12:42
  • 2
    I deleted all the rows, and in the end, 1 item is still visible, BUT the getItemCount = 0. One view still drawn there
    – M.Baraka
    Commented Apr 5, 2017 at 12:44
  • Check the link that is the complete class
    – M.Baraka
    Commented Apr 5, 2017 at 12:53
  • Replace this if(position > -1) with if(position.size != 0) Commented Apr 5, 2017 at 12:59
9

Try this:

placeList.remove(position);
notifyItemRemoved(position);
2
  • please check my edit. Also that is already in my code, i don't see the difference?!
    – M.Baraka
    Commented Apr 5, 2017 at 12:28
  • your solution would work but not so good when user wnats to keep removing. those two lines notify iteme has been removed but does not set the rest of the items' position properly. @Abhishek Kumar solution is complete one. Commented Dec 12, 2022 at 6:16
3

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.

1
  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

1
  • i'm using delete item from recycle view in activity. i'm deleted item in activity then send new array from activity to adapter with this: .notifyDataSetChanged. So all problems gone with this way. Commented Apr 5, 2017 at 12:39
1

I had this issue when I'm using notifyDataSetChanged() it works fine but I wanted the animation so I used notifyItemRemoved(position) for animation then I used notifyDataSetChanged() but I delayed the interval between those functions

I made extension for it you can use it on any RecylcerView

fun RecyclerView.removeItem(position: Int){
   adapter?.notifyItemRemoved(position)
   handler.postDelayed({
       adapter?.notifyDataSetChanged()
   },300)
}
0

for me notifyItemRemoved and notifyDataSetChanged doesnt work. I tried everything , only solution that worked for me is calling or reloading recycler view inside onSwiped method!

0

you don't have to create new reference of place list.that need to declared as array list. you can't add or remove a object from list. you can do that only with arraylist.

change this lines

private List<Place> placeList;

Place place = placeList.remove(position);

to

   private ArrayList<Place> placeList;
// other codes
   placeList.remove(position);
// print placeList to confirm
   notifyItemRemoved(position);
0
fun removeItemPosition(position: Int){
    list.removeAt(position)
    notifyItemRemoved(position)
    notifyItemRangeChanged(position,itemCount)
}
0

just fixed by also notifying that item changed so that list gets updated. notifyItemChanged(position) along with notifyItemRemoved

-3

You need to update you adapter;

Create method in Adapter class where you will be put your data.

For example : setData(List<Place> data);

When last item in list you need to write: adapter.setData(null);

I had the same problem. Just set list = null. It's all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.