我想对这种行为做一点解释:
我创建了一个列表视图。列表视图的每个项目都有一个图像视图,其中填充了来自 URL 的图像。
当我向下滚动到列表时,每个图像都会在项目出现时加载 => OK
但是,当我向上滚动时,之前加载的图像会再次下载。
我怎样才能阻止这种行为?
这是我的下载器的代码:
public class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImageTask(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
这是我的适配器的一段代码:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater();
View rowView;
ArticleHome article = getItem(position);
if (position == 0) {
rowView = inflater.inflate(R.layout.item_ligne_home_premier, null);
new DownloadImageTask((ImageView) rowView.findViewById(R.id.imgimg)).execute(article.getImage());
TextView textView = (TextView) rowView.findViewById(R.id.titlenew);
textView.setText(article.getTitle());
textView.setTypeface(faceLight);
}
else {
rowView = inflater.inflate(R.layout.item_ligne_home, null);
new DownloadImageTask((ImageView) rowView.findViewById(R.id.imgimg)).execute(article.getImage());
TextView title = (TextView) rowView.findViewById(R.id.titlearticleothers);
title.setText(article.getTitle());
title.setTypeface(faceBold);
TextView desc = (TextView) rowView.findViewById(R.id.descriptionarticleothers);
// Add test si < a une certaine longeuryr
desc.setText(article.getDescription().substring(0, 100));
desc.setTypeface(faceLight);
}
// img.setImageAlpha(1680);
// img.setImageAlpha(1880); // Pasmal plus haut plus foncé
return rowView;
}