Listview的ArrayAdapter的notifydatasetchanged()重绘非常慢

发布于 2024-09-24 19:11:09 字数 874 浏览 0 评论 0原文

我有一个非常简单的列表,每行有 3 个文本视图字段。我们每 2 秒左右使用来自后台 Web 服务调用 ( AsyncTask ) 的数据更新它们的值。

我们将即将到来的值与当前值进行比较,在适配器上相应地更新它们,最后在需要时调用 notificationDataSetChanged()

。当我们同时获得超过 3 行更新时,重绘变得非常慢,因此会挂起整个 UI。当然,我们正在使用所有 ListView 众所周知的优化,例如 EfficientAdapter 方法( setTag() 和holder )和 getViewTypecount()/getItemViewType() 。我们还尝试使用layoutopt 尽可能地优化我们的界面,并尝试避免使用wrap_content 宽度和高度来减轻负担。

我们也不会对更新执行昂贵的操作,只是标准操作:更改 TextView 文本、文本颜色和背景颜色值。

我能看到的唯一奇怪的事情是 getView() 每行被调用 3-4-5 次,尽管我已经阅读了所有那些 Romain 的消息 [1] 告诉这没有什么问题关于

如何可以的任何想法或提示我们加快速度?

非常感谢!

[1] http://groups.google。 com/group/android-developers/browse_thread/thread/4c4aedde22fe4594/aeb04288064f495e?show_docid=aeb04288064f495e

I've a quite simple list with 3 textview fields on each row. We are updating their values every 2 seconds or so with data coming from a background webservice call ( AsyncTask )

We compare the coming values with the current ones, update them accordingly on the Adapter and finally calling notifyDataSetChanged() if needed

The thing is that the redraw gets really slow thus hanging the whole UI when we got more than 3 update rows at once. Of course we are using all ListView well-known optimizations such as the EfficientAdapter approach ( setTag() and holders ), and getViewTypecount()/getItemViewType() . We have also tried to optimize our interface as much as possible with layoutopt and trying to avoid wrap_content widths and heights to lighten things up .

We don't do expensive operations on our updates either, just standard stuff: changing TextView text, textcolor, and backgroundcolor values.

The only weird thing I can see is that getView() is called 3-4-5 times for each row, although I've read all those Romain's messages [1] telling that is nothing wrong with that

Any ideas or hints on how can we speed it up?

Thank you very much!

[1] http://groups.google.com/group/android-developers/browse_thread/thread/4c4aedde22fe4594/aeb04288064f495e?show_docid=aeb04288064f495e

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

污味仙女 2024-10-01 19:11:09

这是针对那些从谷歌浏览的人认为他们需要重写自己的数据更改方法的人。根据我的数据,很多情况下你不需要这样做。

notifyDataSetChanged() 比您手动编码的替换要快得多,这一切都取决于您实际的 listview 实现。

示例:一个简单的 3 行纯文本列表视图,最大 10K 行 ArrayList 通过菜单选择进行更新。

手动 notifyDataSetChange()

--- avg run-time: 4ms

默认免费 notifyDataSetChange()

--- avg run-time: 0ms <--- you can't get faster than this.

不要运行来创建自己的替代品,除非您对自己的东西进行计时和基准测试。在必要时使用免费的东西。

This is for those browsing from google thinking they need to rewrite their own data changed method. Based on my data, you don't need to for many cases.

notifyDataSetChanged() can be MUCH FASTER than your hand coded replacement and it all depends on your actual listview implementation.

Sample: A simple 3-line text-only listview with max 10K row ArrayList updated via menu selection.

Manual notifyDataSetChange()

--- avg run-time: 4ms

Default free notifyDataSetChange()

--- avg run-time: 0ms <--- you can't get faster than this.

Don't run to create your own replacement unless you time and benchmark your stuff. Use the free stuff until necessary.

原来是傀儡 2024-10-01 19:11:09

我想您可以将标签设置为您的 textView 作为从中获取更新的 url。您可以尝试对该视图使用 findViewByTag(update URL) 和 setText,而不是调用“notifyDataSetChanged()”,这样 textview 只会被重新绘制,而不是一遍又一遍地重新绘制整个列表。将充分减少额外重涂的次数。只是一个想法。

I suppose you can set a tag to ur textView as the url where it will get the update from. And instead of calling "notifyDataSetChanged()", you can try using findViewByTag(update URL) and setText for that view, so the textview is only repainted not the entire list over and over again. Would sufficiently reduce the number of extra repaints. Just a thought.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文