1.
解决方案:
原理:首先给ImageView设置一个Tag,这个Tag中设置的是图片的url,然后在加载的时候取得这个url和要加载那position中的url对比,如果不相同就加载,相同就是复用以前的就不加载了。
给ImageView设置一个标签
@Override
public Object getItem(int position) {
return URLS[position];
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
//只有当convertView不存在的时候才去inflate子元素
if (convertView == null) {
convertView = getLayoutInflater().inflate(R.layout.single_data,
null);
mHolder = new ViewHolder();
mHolder.mImageView = (ImageView) convertView.findViewById(R.id.image_view);
mHolder.mTextView = (TextView) convertView.findViewById(R.id.text_view);
convertView.setTag(mHolder);
}else {
mHolder = (ViewHolder) convertView.getTag();
}
final String url = URLS[position];
mHolder.mTextView.setText(url != null ? url.substring(url.lastIndexOf("/") + 1) : "");
mHolder.mImageView.setTag(URLS[position]);
if (mDownloader == null) {
mDownloader = new ImageDownloader();
}
//这句代码的作用是为了解决convertView被重用的时候,图片预设的问题
mHolder.mImageView.setImageResource(R.drawable.ic_launcher);
if (mDownloader != null) {
//异步下载图片
mDownloader.imageDownload(url, mHolder.mImageView, "/yanbin",MainActivity.this, new OnImageDownload() {
@Override
public void onDownloadSucc(Bitmap bitmap,
String c_url,ImageView mimageView) {
ImageView imageView = (ImageView) mListView.findViewWithTag(c_url);
if (imageView != null) {
imageView.setImageBitmap(bitmap);
imageView.setTag("");
}
}
});
}
return convertView;
}
/**
* 使用ViewHolder来优化listview
* @author yanbin
*
*/
private class ViewHolder {
ImageView mImageView;
TextView mTextView;
}
}
}