Add Items to top of recyclerview

后端 未结 9 2257
臣服心动
臣服心动 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: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 {
        public int compare(TimelineListData a, TimelineListData b) {
            return Integer.compare(a.position, b.position);
            // you can compare by your own complex formula too
        }
    }
    

提交回复
热议问题