如何使用动态数据实时更新 Android ListView? [英] How to update Android ListView with dynamic data in real time?

查看:26
本文介绍了如何使用动态数据实时更新 Android ListView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个后台线程加载我想在 Android ListView 中显示的数据.数据变化非常频繁(即每秒 1-2 次).有时数据集中的行数也会发生变化(但肯定不会像单元格中的数据变化那样频繁).

I have a background thread loading data which I want to display in an Android ListView. The data changes very often (i.e. 1-2 times per second). Sometimes the number of rows in the dataset changes too (but certainly not as often as the data in the cells changes).

据我所知,有两种方法可以更新单元格中的数据:

There are two ways to update the data in the cells, as far as I can tell:

  1. 让后台线程通知 UI 线程新数据已准备好,然后 UI 线程可以调用 BaseAdapter.notifyDataSetChanged().但是,我在不止一个地方读到过,如果经常调用该方法,它会很慢,因为 ListView 必须重组其所有子视图 Views.

  1. Have the background thread notify the UI thread that new data is ready, and the UI thread can then call BaseAdapter.notifyDataSetChanged(). However, I have read in more than one place that if that method is called often, it will be slow, because the ListView has to restructure all of its subviews Views.

如果数据集计数没有改变,我可能会找到与更改的数据关联的所有可见 ListView 单元格,并手动更新值而无需调用 notifyDataSetChanged().这可能会起作用,但我认为不幸的是,当我通知它时,列表适配器应该为我处理更新通知和机制时,我必须手动更新视图.如果数据集计数随时间变化(即不仅 ListView 的每个单元格内的数据发生变化,而且 ListView 中的单元格总数可以根据提供实时数据的后台线程增加或缩小,则此方法也不起作用).

If the dataset count has not changed, I could possibly find all of the visible ListView cells that are associated with the changed data, and update the values manually without calling notifyDataSetChanged(). This would probably work, but I think its unfortunate that I have to update the views manually when the List Adapter is supposed to handle the update notifications and mechanisms for me when I notify it. This method also won't work if the dataset count changes over time (i.e. not only is the data within each cell of the ListView changing, but the total number of cells in the ListView can grow or shrink based on the background thread supplying realtime data).

我当然会感谢其他实施此方案的人的想法,以及他们如何优化代码的简单性,最重要的是性能.

I would certainly appreciate thoughts from others who have implemented this scenario, and how they optimized code simplicity and most importantly, performance.

推荐答案

我用 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.

这篇关于如何使用动态数据实时更新 Android ListView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆