how to maintain scroll position of listview when it updates

后端 未结 6 1110
[愿得一人]
[愿得一人] 2021-02-05 05:32

I have read plenty of examples ,but if I wish to maintain my scroll position after a ListView is updated from JSON ,then can I do that without using an

6条回答
  •  心在旅途
    2021-02-05 06:32

    In case for some reason you don't want to call notifyDataSetChanged(), the you can maintain the position by using setSelectionFromTop()

    Before updating the adaptor:

    lastViewedPosition = listView.getFirstVisiblePosition();
    
    //get offset of first visible view
    View v = listView.getChildAt(0);
    topOffset = (v == null) ? 0 : v.getTop();
    

    After updating the adaptor:

    listView.setSelectionFromTop(lastViewedPosition, topOffset);
    

提交回复
热议问题