Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remain at a scroll position in RecyclerView after adding items at its first index and call notifydatasetchange

I am using a RecyclerView, I add items before the first item, the scroll position moves up to the newly first item added. How can I maintain my scroll position after adding new items at its first index and call notifydatasetchange() ?

this is what i do in my adapter

    mCurrentFragment.items.addAll(0, createLineItems(dataArrayList));

    notifyDataSetChanged();

Any suggestions ?

like image 786
eddykordo Avatar asked Oct 08 '15 09:10

eddykordo


1 Answers

There are two options:

  1. Use the more sophisticated versions of notify

    List newData = createLineItems(dataArrayList);
    mCurrentFragment.items.addAll(0, newData);
    notifyItemRangeInserted(0, newData.size());
    
  2. Use stable IDs. On your adapter override public long getItemId (int position) to make it return MEANINGFUL values and call setHasStableIds(true); on it.

like image 168
Budius Avatar answered Oct 19 '22 19:10

Budius