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

前端 未结 1 1320
说谎
说谎 2020-12-30 08:07

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

相关标签:
1条回答
  • 2020-12-30 08:38

    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.

    0 讨论(0)
提交回复
热议问题