how to maintain scroll position of listview when it updates

后端 未结 6 1170
[愿得一人]
[愿得一人] 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条回答
  •  萌比男神i
    2021-02-05 06:14

    If you're using an ArrayAdapter (or a subclass of it), the problem may be caused by that the adapter updates the list when you clean it before adding the new items:

    adapter.clear();
    adapter.addAll(...);
    

    You can fix it by wrapping the code that modifies the adapter like this:

    adapter.setNotifyOnChange(false); // Disable calling notifyDatasetChanged() on modification
    adapter.clear();
    adapter.addAll(...); // Notify the adapter about that data has changed. Note: it will re-enable notifyOnChange
    adapter.notifyDataSetChanged();
    

提交回复
热议问题