Restore the scroll position of RecyclerView

You may have encountered this situation during the development process. After the Activity/Fragment was recreated, the RecyclerView lost the scroll position information it had before. Usually this happens because the Adapter data is loaded asynchronously, and the data has not been loaded yet when the RecyclerView needs to be laid out, causing the RecyclerView to fail to restore to the previous scroll position.

Starting from version 1.2.0-alpha02  , Jetpack RecyclerView provides a new API that allows the Adapter to block the layout behavior before the data is loaded , so as to avoid losing scroll position information. Next, we will introduce how to use this new API and how it works.

  • 1.2.0-alpha02 version

    https://developer.android.google.cn/jetpack/androidx/releases/recyclerview

Restore to original scroll position

There are several methods to restore the RecyclerView to the correct scroll position, and you may have used these methods in actual projects. One of the best methods is to cache the data in memory, ViewModel or Repository in advance, and then make sure to set the cached data to the Adapter before the first layout is passed in. If this method cannot be used according to the actual situation of your project, you can also use other methods, but it is either more complicated (for example, avoid setting the Adapter in RecyclerView, but this may cause display problems like headers and other items), or Will cause the LayoutManager.onRestoreInstanceState API to be abused.

  • LayoutManager.onRestoreInstanceState

    https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager?hl=en#onRestoreInstanceState(android.os.Parcelable)

The solution provided in the recyclerview: 1.2.0-alpha02 version is to introduce a new Adapter method to allow you to set its state recovery strategy (through the enumerated type StateRestorationPolicy). It has three options:

  • ALLOW — the default state, the RecyclerView state will be restored immediately when the next layout is completed;

  • PREVENT_WHEN_EMPTY — Only when the adapter is not empty (ie adapter.getItemCount()> 0), the state of RecyclerView will be restored. If you load data asynchronously, RecyclerView will wait for the data to be loaded before restoring the state. If there are some default items in the Adapter, such as header or load progress indicator, you should use the PREVENT option, unless the default item is added through ConcatAdapter. For more details, please refer to " Use ConcatAdapter to Connect to Other Adapters in Order " . ConcatAdapter will wait for all adapters to be ready before restoring the state;

  • PREVENT — All state restoration will wait until you set the ALLOW or PREVENT_WHEN_EMPTY option before being executed.

  • StateRestorationPolicy

    https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.Adapter.StateRestorationPolicy

  • ALLOW

    https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.Adapter.StateRestorationPolicy#ALLOW

  • PREVENT_WHEN_EMPTY

    https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.Adapter.StateRestorationPolicy#PREVENT_WHEN_EMPTY

  • ConcatAdapter

    https://developer.android.google.cn/reference/androidx/recyclerview/widget/ConcatAdapter

  • PREVENT

    https://developer.android.google.cn/reference/androidx/recyclerview/widget/RecyclerView.Adapter.StateRestorationPolicy#PREVENT

The adapter's state recovery strategy can be set through the following sample code:

adapter.stateRestorationPolicy = PREVENT_WHEN_EMPTY

You can learn about RecyclerView's lazy state restoration (lazy state restoration) function through this short and concise article. Hurry up and start using it!


Recommended reading




 Click the screen at the end read read the original text  | Use RecyclerView create a list  


Guess you like

Origin blog.csdn.net/jILRvRTrc/article/details/108819456