ListView的性能优化网上很多信息,但是涉及到异步加载图片问题就会出现问题。
具体参看上篇文章http://314858770.iteye.com/admin/blogs/1217594
如果每次都重新inflate一个新的View出来肯定会造成性能损失严重,可能会出现listview滚动是很卡的情况,还会出现内存溢出。
现在想出一个方法就是每次都添加一个标识,然后设置图片的时候检查这个标识,如果不相符则忽略此次。
适配器中的关键代码
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if(convertView == null){
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.listview_item, null);
viewHolder.imageView = (ImageView)convertView.findViewById(R.id.imageView);
viewHolder.textView = (TextView)convertView.findViewById(R.id.textView);
convertView.setTag(viewHolder);
}
viewHolder = (ViewHolder)convertView.getTag();
viewHolder.textView.setText(stringList.get(0)[0]);
final ImageView imageView = viewHolder.imageView;
imageView.setImageDrawable(null); //reset default
imageView.setTag(position);
asyncImageLoader.loadDrawable(stringList.get(0)[1], new AsyncImageLoader.ImageCallback() {
@Override
public void imageLoaded(Drawable imageDrawable, String imageUrl,int key) {
if((Integer)imageView.getTag() != key) return; //不是当前显示的View,则忽略此次
if(imageDrawable != null){
imageView.setImageDrawable(imageDrawable);
}
}
},position);
return convertView;
}
static class ViewHolder{
TextView textView;
ImageView imageView;
}
异步加载图片的代码
public Drawable loadDrawable(final String imageUrl,
final ImageCallback callback,final int key) {
if (!URLUtil.isValidUrl(imageUrl)) {
callback.imageLoaded(null, imageUrl,key);
return null;
}
if (imageCache.containsKey(imageUrl)) {
SoftReference<Drawable> softReference = imageCache.get(imageUrl);
if (softReference.get() != null) {
Log.d(AsyncImageLoader.class.getName(),
"imageCache has this image,use cache. the url:"
+ imageUrl);
callback.imageLoaded(softReference.get(), imageUrl,key);
return softReference.get();
}
}
//更多代码
}
public interface ImageCallback {
public void imageLoaded(Drawable imageDrawable, String imageUrl,int key);
}
经过此次修改,再测试时不会出现之前的内存持续增长 和 滚动很卡的情况。