问题描述
我有一个的ListView
自定义 ArrayAdapter
。每一个在这个的ListView
的行都有一个图标和一些文字。这些图标下载在后台,缓存,然后使用一个回调,在各自的 ImageViews
取代。获得从高速缓存或下载缩略图的逻辑每次触发 getView
()运行。
I have a ListView
with custom ArrayAdapter
. Each of the row in this ListView
has an icon and some text. These icons are downloaded in background,cached and then using a callback, substituted in their respective ImageViews
. The logic to get a thumbnail from cache or download is triggered every time getView
() runs.
现在,根据罗曼盖伊:
是绝对没有保证的 的顺序getView()将是 所谓也不会有多少次。
我已经看到这种情况发生,对于连续两个大小getView()被调用六次!的
I have seen this happen, for a row of size two getView() was being called six times!
如何更改我的code,以避免重复缩略图取-请求,并处理视图回收?
How do I change my code to avoid duplicate thumbnail-fetch-requests and also handle view recycling?
感谢。
推荐答案
从API。
public abstract View getView (int position, View convertView,
ViewGroup parent)
convertView - 旧视图重用,如果可能的话。注意:您应该检查这个观点是在使用前非空一个合适的类型和。如果它是不能转换该视图以显示正确的数据,该方法可以创建新的视图。
convertView - The old view to reuse, if possible. Note: You should check that this view is non-null and of an appropriate type before using. If it is not possible to convert this view to display the correct data, this method can create a new view.
所以,如果getView已经呼吁这个特定的指数则convertView将是从第一次调用返回的视图对象。
So if getView has already been called for this specific index then convertView will be the View object that was returned from that first call.
您可以这样做。
if(!(convertView instanceof ImageView)){
convertView = new ImageView();
//get image from whereever
} else {} // ImageView already created
这篇关于处理多个getView最好的方式,从一个适配器内部调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!