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 ?
There are two options:
Use the more sophisticated versions of notify
List newData = createLineItems(dataArrayList);
mCurrentFragment.items.addAll(0, newData);
notifyItemRangeInserted(0, newData.size());
Use stable IDs. On your adapter override public long getItemId (int position)
to make it return MEANINGFUL values and call setHasStableIds(true);
on it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With