How to save and restore ListView position in Android

前端 未结 4 605
再見小時候
再見小時候 2021-02-03 15:05

I have tabView, displaying list in each tab. When I scroll in one tab, switch to another tab and return to previous tab, position is returned to the top instead of displaying pr

4条回答
  •  你的背包
    2021-02-03 15:20

    Override onSaveInstanceState to save the position, and set the position back in onRestoreInstanceState. The Bundle is sort of like a HashMap, (pseudo-code):

    In onSaveInstanceState:

    bundle.putInt("MyTabsPosition", getPosition());
    

    Then in onRestoreInstanceState:

    pseudo.setPosition(bundle.getInt("MyTabsPosition"));
    

提交回复
热议问题