Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

notifyItemInserted() not working as I was expecting

Tags:

android

What I need to do is inserting item to the top of RecyclerView. I have adapter which persists List of my items and when I want to add to the top of RecyclerView I'm simply using this code:

mItems.add(0, item);
notifyItemInserted(0)

Unfortunately it's just reloading last item of RecyclerView. Of course when I change notifyItemInserted to notifyDataSetChanged() everything works fine. Why is notifyItemInserted not appropriate?

like image 616
Nominalista Avatar asked May 16 '17 14:05

Nominalista


2 Answers

Just remove setHasFixedSize(true) from your initialization of RecyclerView.

like image 53
Djek-Grif Avatar answered Nov 09 '22 16:11

Djek-Grif


mItems.add(0, item);

if(mItems.size()==1){

   notifyDataSetChanged();
} else {

notifyItemInserted(position);

}
like image 36
Namdev Deshmukh Avatar answered Nov 09 '22 18:11

Namdev Deshmukh