I have a Recyclerview in my activity. when I pull down it will load new items to recycle view. Now I need to implement pull to refresh the concept to my recyclerview. I have
I also need to add items to the front of recyclerview(and to bottom), but i need to keep scroll focused at the previous top item.
So i'm scrolling recyclerview to previous top item:
mAdapter.pushFront(items);
mAdapter.notifyItemRangeInserted(0, items.size());
recyclerView.scrollToPosition(items.size() - 1);
Is there a better solution?
I would insist you to add item at 0th
position which is coming from pull to refresh as below,
mArrayList.add(position, item);
notifyItemInserted(position);
Recycler view has nothing to with ordering of items. From the above code you are refreshing contents and simply displaying what you are getting from server. May be the items returned from server are in the order they get displayed. So check the order from server what you are getting.
You can reverse your whole list using Collections :
Collections.reverse(historyitems);
Try using adapter :
adapter.insert(yourItem, 0);
Try using List :
list.add(0,listItem);
Do it in xml you don't need LinearLayoutManager
code anymore
<android.support.v7.widget.RecyclerView
android:id="@+id/recordItemList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clipToPadding="false"
android:scrollbars="none"
app:layoutManager="LinearLayoutManager"
app:stackFromEnd="true"
app:reverseLayout="true"/>
For perfect control position system, add each item's position in arraylist. For new item position will be 0. Use Comparator and sort items by using Collection Class.
Collections.sort(arrayOfPosts, new SortPostByPosition());
listAdapter.notifyDataSetChanged();
Here is SortPostByPosition class that compare position of each item.
class SortPostByPosition implements Comparator<TimelineListData> {
public int compare(TimelineListData a, TimelineListData b) {
return Integer.compare(a.position, b.position);
// you can compare by your own complex formula too
}
}