Better to recreate listview adapter or to clear and repopulate?

倾然丶 夕夏残阳落幕 提交于 2020-01-13 09:17:04

问题


If I have a ListView with a CustomAdapter and let's say I have this refresh() method that refreshes the list with new results, should I:

  1. Call new CustomAdapter(...) upon initialization, and every time I call refresh(), I use adapter.clear() and adapter.add(...)

  2. Call new CustomAdapter(...) every time I call refresh()

Basically, I'm asking, is it better to recreate an adapter everytime I load new results or is it better to clear the results in existing adapter and add the entire list to it?


回答1:


It is definitely better to call notifyDataSetChanged() on the original adapter than setting a new one.

The reason is performance: ListView uses view recycling to avoid creating new item views as you scroll. When you set a new adapter, these recycled views are discarded, which means they must be recreated from scratch for the next layout pass. Take a look at the code of ListView.setAdapter():

@Override
public void setAdapter(ListAdapter adapter) {
    if (mAdapter != null && mDataSetObserver != null) {
        mAdapter.unregisterDataSetObserver(mDataSetObserver);
    }

    resetList();
    mRecycler.clear();

    ...

This is completely logical behavior, since the ListView supposes that the views the new adapter will use are incompatible with the ones returned by the previous adapter (in any case, it cannot assume that they will be compatible). So they're thrown away.

Therefore, if you set a new adapter each time, you're incurring an unnecessary performance cost (recreating all the current views).

Also, if you wrote a custom adapter, you don't necessarily have to call add() individually (as with, say, ArrayAdapter). You can just replace the internal data collection with the new one and call notifyDataSetChanged() afterwards.




回答2:


I think if you're going to use adapters as they were intended, then you'll update the adapter's data and call notifyDataSetChanged() on it.

If you look at the API for something like ArrayAdapter, it has numerous methods like clear(), add(), addAll(), notifyDataSetChanged() and so forth that are there to guide you towards a certain use of the API.

By using these methods, not only does it make your code compliant with its intended usage, it also makes it easier to understand and familiar to others that are trying to understand your code.

So basically, I would only recreate the adapter as a last resort.



来源:https://stackoverflow.com/questions/24835969/better-to-recreate-listview-adapter-or-to-clear-and-repopulate

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