ListView refresh taking it to the top of the list

不想你离开。 提交于 2019-12-24 18:13:06

问题


Scenario: I am using a listview with custom Adapter. I am fetching data every 5 seconds and then displaying it in the listview.

Problem: After every refresh the list scrolls at the top. I would want the list to stay as is from the scroll standpoint and just the underlying data to be refreshed.

Current code: This is called every time I fetch a new data.

mListAdapter = new CustomListAdapter(this, mListData);
mList.setAdapter(mListAdapter);

回答1:


Instead of your code please try to put every time you fetch data this one:

adapter.notifyDataSetChanged();



回答2:


Instead of calling this, you should set the new data to the adapter and then call notifyDataSetChanged.

Hope this helps!




回答3:


As you get new mListData then just invalidate the adapter not re-initialized the adapter you can call like this

mListAdapter.notifyDataSetChanged();



回答4:


You can use

mList.setSelection(mListData.size() - CONSTANT.CONTENT_SIZE);

This way the listview will not refresh to the top. Or you can also use

adapter.notifydatasetchanged

by updating the binded list.




回答5:


This Worked For Me:

adapter.setData(list);
adapter.notifyDataSetChanged();


来源:https://stackoverflow.com/questions/8563099/listview-refresh-taking-it-to-the-top-of-the-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!