1

My question is based on this post.

Android. How does notifyDataSetChanged() method and ListViews work?

I've read in various articles that to refresh the listView you must call notifyDataSetChanged() and not listView.setAdapter(new Adapter(....)); because the second method is too costly and affects performance

The answer to the above question says that adapter.notifyDataSetChanged() affects the views that are currently visible on the screen. So getView() is called as many times as the number of items currently displayed.

But getView() is called the same number of times when assigning a new adapter to the listView as well.

So what is the difference between calling adapter.notifyDataSetChanged() and listView.setAdapter(new Adapter(....));?

4

2 に答える 2

3

を呼び出すとnotifyDataSetChanged()getView()が同じ回数呼び出されます。ただし、アダプターは同じであるため、これらのビューは再利用できます (つまり、 として渡されますconvertView)。

ListViewこれは、新しいアダプタが同じレイアウトを使用していることを確実に認識できないため、新しいアダプタを提供するときには実行できません。したがって、リサイクラーはクリアされ、すべての行を最初から作成する必要があります (再利用するよりもはるかにコストがかかります)。

convertView(提供されたビューを無視し、常に新しいビューを作成/拡張する場合、このパフォーマンス ポイントは意味がありませんが、いずれにせよ、それは悪い考えです)。

于 2014-06-09T15:23:20.380 に答える
1

setAdapter() は、convertView として保存するために使用されるスクラップ ヒープをクリアし、新しいアダプターも設定してから、requestLayout() を実行します。

ただし、notifyDataSetChanged() は requestLayout() だけです。そのため、スクラップ ヒープはまだ存在し、getView() が発生すると、スクラップ ヒープに含まれている場合は convertView が null にならないため、インフレートする必要はありません。

したがって、データが変更された場合は、setAdapter() ではなく、notifyDataSetChanged() を呼び出す方が効率的です。

于 2015-08-27T02:17:16.070 に答える