如何更新的Andr​​oid的ListView进行实时动态数据?实时、动态、数据、Andr

2023-09-05 06:34:06 作者:花香袭人

我有我想要在Android的ListView显示后台线程加载数据。这些数据变化非常频繁(即1-2次每秒)。有时在数据集中的行数也会发生变化(但肯定不是尽可能经常在细胞的变化数据)。

有两种方法来更新单元格中的数据,据我可以告诉:

有后台线程通知UI线程新的数据已经准备好,然后UI线程可以调用BaseAdapter.notifyDataSetChanged()。不过,我已经读过不止一个地方,如果该方法通常被称为,这将是缓慢的,因为ListView中有重组其所有子视图查看。

如果数据集数没变,我可能找到所有与该改变的数据相关联的可见的ListView细胞,并手动而不调用notifyDataSetChanged更新值()。这可能会工作,但我认为它不幸的是,我必须手动更新视图时,列表适配器应该处理的更新通知和机制,我当我通知吧。这种方法也将不能工作,如果随着时间的推移数据集计数变化(即不仅是在ListView变化的每个小区内的数据,但细胞在ListView总数可以增大或缩小基于所述后台线程供给的实时数据)。

我当然AP preciate从别人的想法谁已实施此方案,以及如何优化code简单,最重要的性能。

解决方案

我尝试用的ListView ,你实际上必须更新的ListView 手动细胞,而不调用 notifyDataSetChanged()如果你有实时数据,并想在的ListView 来更新具有更好的性能。

notifyDataSetChanged()引起的ListView 来重建其整个查看层次是很慢的,如果你在呼唤它频繁地(即每秒一次)。

注意(2014年)。这确实如果您使用的是普通的现代的ListView有ViewHolder模式不适用。您只需拨打notifyDataSetChanged,这一切就是这么回事。这是令人难以置信的高效率成为Android知道只更新屏幕上的细胞。

我实现了一个方案,即如果我的数据集的大小从一个更新更改为下一个,我称之为 notifyDataSetChanged()。如果数据集大小保持不变,从一个更新到下一个(使得细胞在的ListView 的数目是相同的,当需要的数据的一个重画之前)仅 getLastVisiblePosition(),并更新可见单元格

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:

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.

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.

解决方案

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() causes the ListView to rebuild its entire View hierarchy is very slow if you are calling it frequently (i.e. once every second).

Note (2014). This DOES NOT APPLY if you are using normal modern ListView with a ViewHolder pattern. You simply call 'notifyDataSetChanged' and that's all there is to it. It is incredibly efficient as Android knows to only update the cells on the screen.

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.