Add Items to top of recyclerview

后端 未结 9 2069
臣服心动
臣服心动 2021-01-04 04:56

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

相关标签:
9条回答
  • 2021-01-04 05:19

    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?

    0 讨论(0)
  • 2021-01-04 05:21

    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); 
    
    0 讨论(0)
  • 2021-01-04 05:27

    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.

    0 讨论(0)
  • 2021-01-04 05:36

    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);
    
    0 讨论(0)
  • 2021-01-04 05:37

    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"/>
    
    0 讨论(0)
  • 2021-01-04 05:38

    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
        }
    }
    
    0 讨论(0)
提交回复
热议问题