将项目添加到 ListView 而不重新加载
我想知道是否有一种方法可以将项目添加到 ListView 而不会导致整个列表的重新加载。
我有一个 BaseAdapter 派生的 ListView 适配器,当底层模型添加新元素时,它会调用 notificationDataSetChanged(),这会触发 ListView 重新加载。
列表中的元素具有图像,这些图像根据元素的内容动态加载。问题是,当在重新加载期间调用 getView() 时,为重用而传递的 ConvertView 参数来自之前的不同位置,因此图像也必须重新加载,这会导致相当难看的闪烁。
那么,如果我只在末尾添加一个项目(这将是添加新项目的唯一方法),有没有办法不重新加载整个列表?或者如果可能的话,至少以某种方式重复使用同一位置的单元格,以避免昂贵的图像重新加载?
I'm wondering if there's a way to add an item to the ListView without causing the reload of the whole list.
I have a BaseAdapter-derived ListView adapter, and when the underlying model gets a new element added, it calls notifyDataSetChanged(), which triggers the ListView reload.
The elements in the list have images, which are dynamically loaded according to the element's content. Problem is, when getView() is called during the reload, the convertView parameter passed for reuse is from a different position previously, so the images have to be reloaded, too, which causes a rather ugly blinking.
So is there a way to not reload the whole list if I only add an item at the end (and that will be the only way new items are added)? Or at least somehow reuse the cells for the same position, if possible, to avoid the costly image reload?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
没有伙伴在android中你不可能在不刷新列表的情况下添加一个项目,因为当你添加一个项目时它会改变列表视图的高度。请参阅下面的链接,Romain Guy在其中说了同样的事情。
http://groups.google.com/group/android-developers/browse_thread/thread/7e54522c37772d04/05ab fd17b59b07f7?lnk=gst&q=how+to+add+list+item+in+listview+without+reload+the+listview+#05abfd17b59b07f7
No buddy its not possible in android that u can add an item without refresh the list because when u add an item it changes the height of list view.See the below link,Romain Guy said the same thing in it.
http://groups.google.com/group/android-developers/browse_thread/thread/7e54522c37772d04/05abfd17b59b07f7?lnk=gst&q=how+to+add+list+item+in+listview+without+reload+the+listview+#05abfd17b59b07f7
这样做而不是调用notifyDataSetChanged():
do this instead of calling notifyDataSetChanged():
嗯,这是可能的。
重写 ArrayAdapter 的 getView 和 add 方法
我做了什么,例如测试用例:
}
第一次加载所有项目,并且图像由异步线程动态加载。 getView 方法中的以下代码部分可防止列表完全刷新,从而防止图像闪烁:
当将新项目添加到列表中时,ArrayAdapter 将再次遍历列表。只需检查当前位置的item是否是新添加的item即可。如果没有,则返回旧视图的convertView,否则加载该项目并返回自定义视图。
Well, it is possible.
Override the methods getView and add of the ArrayAdapter
What i did for example as testcase:
}
The first time it loads all the items and the images are loaded dynamically by the Async thread. The following part of the code in method getView prevents the list from refreshing it completely, so preventing the blinking of the images:
The ArrayAdapter will go through the list again when a new item is added to the list. Just check whether the item at the current position is the newly added item. If not then return the convertView which is the old view or else load the item and return the custom view.
如果您将适配器设置为具有稳定的 id,那么它应该阻止为调用
notifyDataSetChanged()
之前已经可见的视图调用getView(...)
。这可以通过重写hasStableIds()
使其返回true
来完成,然后确保您的getItemId(...)
实现是实际上返回一个稳定且唯一的 id。If you set your adapter to have stable ids then it should prevent
getView(...)
from being called for the views that were already visible before callingnotifyDataSetChanged()
. This can be done by overridinghasStableIds()
so that it returnstrue
and then making sure that your implementation ofgetItemId(...)
is actually returning a stable and unique id.