RecyclerView notifyItemRemoved(position) not working properly
Asked Answered
D

10

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

Dinger answered 5/4, 2017 at 12:18 Comment(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 listSosna
@Vivek Mishra Normal behavior where i click on delete, it works without the notifyItemRangeChanged(). But If i go to new view, go back, >> bug happensDinger
I called reloaded recycler view inside onSwiped method. It worked! Though it will remove the animation.Correia
D
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());
            }

         }
    });
Denmark answered 5/4, 2017 at 12:30 Comment(7)
I did log size before i remove & after, List size is not 0Dinger
You mean you deleted all the rows, still you get one in your list size.Denmark
I deleted all the rows, and in the end, 1 item is still visible, BUT the getItemCount = 0. One view still drawn thereDinger
Check the link that is the complete classDinger
Replace this if(position > -1) with if(position.size != 0)Denmark
Let us continue this discussion in chat.Denmark
I couldn't find the reason. I ended up re-writing the whole class, and it works now for some reason.Dinger
W
9

Try this:

placeList.remove(position);
notifyItemRemoved(position);
Withy answered 5/4, 2017 at 12:22 Comment(2)
please check my edit. Also that is already in my code, i don't see the difference?!Dinger
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.Textuary
H
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.

Hermes answered 11/3, 2020 at 15:19 Comment(0)
H
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

Havens answered 5/4, 2017 at 12:21 Comment(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.Literally
A
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)
}
Arana answered 15/5, 2022 at 10:8 Comment(0)
C
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!

Correia answered 26/8, 2021 at 5:49 Comment(0)
S
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);
Shipmaster answered 26/8, 2021 at 6:14 Comment(0)
P
0
fun removeItemPosition(position: Int){
    list.removeAt(position)
    notifyItemRemoved(position)
    notifyItemRangeChanged(position,itemCount)
}
Physoclistous answered 17/9, 2023 at 3:15 Comment(0)
A
0

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

Arrowroot answered 7/4, 2024 at 4:43 Comment(0)
L
-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.

Lolland answered 5/4, 2017 at 12:29 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.