Android ListView ArrayAdapter数据更新最佳实践
我有一个具有多个列表视图的活动,这些视图不断从套接字线程接收新值,另一个线程解析数据并更新数组适配器,然后 ui 线程调用notifyDataSetChanged() 来刷新列表。
我的问题是,我每秒刷新所有列表几次,这会导致用户界面在需要发生某些动画时非常滞后。
我想知道每秒更新多个值更改的多个列表的最佳方法是什么?
谢谢, 图腾。
I have an activity with multiple list views that are continuously receiving new values form a socket thread, another thread parses the data and updates the array adapters, then the ui thread calls notifyDataSetChanged() to cause the list to refresh.
My issue is that im refreshing all the list a couple of time a second, this causes the UI to be very laggy when some animations need to happen.
I was wondering what the best way is to update multiple lists with multiple value changes every second?
Thanks,
Totem.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我肯定会遵循他们今年在 Google IO 上给出的指导方针。
Google IO 列表视图视频
I would definately follow the guidelines they gave at Google IO this year.
Google IO listview Video
您应该使用 Cursors(如果需要内容提供程序)和 ListActivity。一旦发生更改,UI 就会自动更新,如果数据集为空,列表会自动显示相关视图。
以下示例使用内容提供程序解决该问题:
main.xml:
注意 android:list 和 android:empty 标记。这些是列表活动所必需的。
在 onCreate() 方法中:
如果您的视图很简单,您可以使用 SimpleCursorAdapter。由于视图复杂,我使用自己的适配器创建:
You should use Cursors (if required Content Providers) and ListActivity. The UI automatically updates as soon as there are changes and in case of null data sets, the list automatically displays a relevant view.
Following examples solves it using content providers:
main.xml:
Notice the android:list and android:empty tags. These are required by the list activity.
In onCreate() method:
You can use a SimpleCursorAdapter if your views are straight-forward. I created by own adapter because of the complex views:
是的,该视频非常有帮助。最大的收获之一是您应该回收传递到列表适配器 getView 方法中的 ConvertView:
另一个有用的一点是使用 ViewHolder 类进行视图回收。视频的时间约为 10:30。
Yes, that video is very helpful. One of the biggest take aways is that you should recycle the convertView passed into your list adapters getView method:
The other helpful bit was to use a ViewHolder class for the view recycling. It's at ~10:30 into the vid.