我有一个 ArrayList,它经常变化。
ListView 显示 ArrayList 的数据,因此当 ArrayList 更改时,必须快速更改此 ListView。
所以我写了这样的代码:
ArrayList ar;
ListView lv;
paintmethod()
{
lv.items.clear();
lv.addlistview(ar);
lv.invalidate();
}
private void addlistview(ArrayList arr)
{
lv.Items.Add(arr.somedata.tostring(),arr.somedata.tostring());
}
此代码有效,但当 ArrayList 更改时,ListView 不会立即刷新。
20秒、30秒甚至1分钟后刷新。
我该如何做更多的事情来解决这个问题?
最佳答案
尝试下面的内容,看看是否有更好的效果。使用 BeginUpdate() 是一个很好的做法和 EndUpdate()更新多个 listView 项目时。因为 BeginUpdate() 会阻止控件绘制,直到调用 EndUpdate 方法为止。
paintmethod()
{
lv.BeginUpdate();
lv.items.clear();
lv.addlistview(ar);
lv.EndUpdate();
}
The preferred way to add multiple items to a ListView is to use the AddRange method of the ListView.ListViewItemCollection (accessed through the Items property of the ListView). This enables you to add an array of items to the list in a single operation.
这应该会大大提高性能。
关于c# - C# 中的 Listview 慢刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7327943/