how to maintain scroll position of listview when it updates

后端 未结 6 1163
[愿得一人]
[愿得一人] 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:30

    It is easier to maintain scroll position by calling notifydatasetchanged() only. The problem there is that you are creating a new adapter every time the data gets updated... you should do something like this:

    if(listView.getAdapter()==null)
       listView.setAdapter(myAdapter);
    else{
       myAdapter.updateData(myNewData);  //update adapter's data
       myAdapter.notifyDataSetChanged(); //notifies any View reflecting data to refresh
    }
    

    This way, your listview will mantain the scrolling position.

    In case you want to scroll to a new position, use:

    list.smoothScrollToPosition(int position);
    

提交回复
热议问题