Maintain/Save/Restore scroll position when returning to a ListView

后端 未结 20 1655
無奈伤痛
無奈伤痛 2020-11-21 21:30

I have a long ListView that the user can scroll around before returning to the previous screen. When the user opens this ListView again, I want the

20条回答
  •  既然无缘
    2020-11-21 22:16

    You can maintain the scroll state after a reload if you save the state before you reload and restore it after. In my case I made a asynchronous network request and reloaded the list in a callback after it completed. This is where I restore state. Code sample is Kotlin.

    val state = myList.layoutManager.onSaveInstanceState()
    
    getNewThings() { newThings: List ->
    
        myList.adapter.things = newThings
        myList.layoutManager.onRestoreInstanceState(state)
    }
    

提交回复
热议问题