notificationDataSetChanged() 性能问题以及何时/如何调用与一批数据更改相关的问题
我有自定义对象的 ArrayList,它是 ListView 的 ArrayAdapter 底层的数据。
有时,这些数据会被批量修改,例如从网络上获取一组新项目。当批量修改数据时,是否应该在每次 add() 到 ArrayList 之后调用 notificationDataSetChanged()
一些过于简化的代码:
for(Object object : newObjects){
list.add(object);
adapter.notifyDataSetChanged();
}
或者应该在批量中的所有项目都已修改后调用一次已添加?
for(Object object : newObjects){
list.add(object);
}
adapter.notifyDataSetChanged()
假设有一批 50 个新对象。如果连续进行 50 次 notificationDataSetChanged() 调用(如第一个示例中所示),视图是否会连续重绘自身 50 次(我想象性能会受到重大影响),还是仅执行最新的调用,并且在某种意义上仅重绘他们一次?
我基本上想知道我是否可以使用第一种方法或者它会对性能产生重大影响吗?
谢谢
I have ArrayList of custom objects that is the data underlying an ArrayAdapter for a ListView.
Sometimes this data is modified in a batch, such as fetching a set of new items from the web. When the data is modified in a batch, should notifyDataSetChanged() be called after every add() to the ArrayList
Some over simplified code:
for(Object object : newObjects){
list.add(object);
adapter.notifyDataSetChanged();
}
or should it be called once after all of the items in the batch have been added?
for(Object object : newObjects){
list.add(object);
}
adapter.notifyDataSetChanged()
Say there is a batch of 50 new objects. If 50 notifyDataSetChanged() calls are made right after another, like in the first example, will the views redraw themselves 50 times in a row (I imagine a major performance hit) or will it only perform the latest call and in a sense only redraw them once?
I'm basically wondering if I can use the first method or will it have a major performance impact?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您调用notifyDataSetChanged()时,它不会立即重绘视图。由 UI 线程控制器决定何时重绘。尽管它看起来是瞬时的。您应该尝试一下,看看是否存在性能问题。我们确实无法为您解答。如果在所有添加之后只更新列表视图是有意义的,那么就这样做。
When you call notifyDataSetChanged(), it doesn't redraw the view immediately. It's up to the UI thread controller to know when to redraw. Although it does appear instantanious. You should try it and see if there's a performance issue. We really can't answer that for you. If it makes sense to only update the listview after all the adds, then do that.