【发布时间】:2015-09-04 09:55:02
【问题描述】:
我有一个图像列表视图。
如果本地缓存中不存在图像,则占位符图像将保留在图像的位置。
然后运行一个异步任务从服务器下载图像。
一旦异步任务完成,我将 imageView 位图设置为下载的图像。
然后我打电话给notifyDataSetChanged()
问题是我需要向下滚动并向上滚动每个单元格以显示已下载的新图像。它不会自动更新单元格。
所以我有:
public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {
private ImageView imageView;
public ImageLoadTask(ImageView imageView) {
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(Void... params) {
// download image from server and return
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
imageView.setImageBitmap(result)
notifyDataSetChanged()
}
}
在我的 getView() 方法中,我有:
if(image exists in cache){
myImageView.setBitmap(image in Cache);
}else{
new ImageLoadTask(myImageView).execute()
}
因此,存储在缓存中的任何图像都可以正常工作。
任何必须首先下载的图像都不会正确更新,除非您向下滚动并再次备份。
有什么问题?
【问题讨论】:
-
我建议您使用 volley 或 Picasso 库来加载图像。
-
需要进行如下操作 //清除现有项
listAdapter.clear();//添加新项listAdapter.addAll(updatedvalues);//通知数据发生变化listAdapter.notifyDataSetChanged();
标签: android image asynchronous android-listview