更新时间:2023-01-22 16:09:30
我用 ListView
进行了实验,实际上你必须手动更新 ListView
单元格而不调用 notifyDataSetChanged()
如果您有实时数据并且希望 ListView
以更好的性能进行更新.
I experimented with ListView
, and you essentially have to update the ListView
cells manually without calling notifyDataSetChanged()
if you have realtime data and you want the ListView
to update with better performance.
notifyDataSetChanged()
导致 ListView
重建其整个 View
层次结构如果您经常调用它(即每秒一次).
notifyDataSetChanged()
causes the ListView
to rebuild its entire View
hierarchy is very slow if you are calling it frequently (i.e. once every second).
注意(2014 年).如果您使用带有 ViewHolder 模式的普通现代 ListView,则此方法不适用.您只需调用notifyDataSetChanged"即可.它非常高效,因为 Android 知道只更新屏幕上的单元格.
我实现了一个方案,如果我的数据集大小从一个更新更改为下一个更新,我调用 notifyDataSetChanged()
.如果数据集大小从一次更新到下一次更新保持不变(使得 ListView
中的单元格数量与之前需要重绘数据时相同),那么我迭代ListView.getFirstVisiblePosition()
: getLastVisiblePosition()
,只更新可见单元格.
I implemented a scheme where if my data set size changed from one update to the next, I call notifyDataSetChanged()
. If the data set size remained constant from one update to the next (such that the number of cells in the ListView
is the same as before when a redraw of the data is needed), then I iterate over the ListView.getFirstVisiblePosition()
: getLastVisiblePosition()
, and update the visible cells only.