我想创建一个 View 列表,其中每个 View 中显示的图像都是在您滚动列表时从服务器下载的(延迟加载)。这是我到目前为止得到的代码:
public class CustomAdapter extends ArrayAdapter<Component> {
private final List<Component> components;
private final Activity activity;
public CustomAdapter(Activity context, List<Component> components) {
super(context, android.R.layout.simple_list_item_1, components);
this.components = components;
this.activity = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.item, null, false);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder)convertView.getTag();
}
Component component = components.get(position);
// Don't show any image before the correct one is downloaded
viewHolder.imageView.setImageBitmap(null);
if (component.getImage() == null) { // Image not downloaded already
new DownloadImageTask(viewHolder.imageView).execute(component);
} else {
viewHolder.imageView.setImageBitmap(component.getImage());
}
return convertView;
}
private class ViewHolder {
ImageView imageView;
}
private class DownloadImageTask extends AsyncTask<Component, Void, Component> {
private ImageView imageView;
public DownloadImageTask(ImageView imageView) {
this.imageView = imageView;
}
@Override
protected Component doInBackground(Component... params) {
String url = params[0].getImageURL();
Component component = params[0];
// Download the image using the URL address inside found in component
Bitmap image = ImageDownloader.getImage(url);
// Set the Bitmap image to the component so we don't have to download it again
component.setImage(image);
return component;
}
@Override
protected void onPostExecute(Component component) {
// Update the ImageView with the downloaded image and play animation
imageView.setImageBitmap(component.getImage());
Animation animation = AnimationUtils.loadAnimation(activity, R.anim.fade_in);
imageView.startAnimation(animation);
}
}
}
基本上,当 getView() 运行时,它从组件(用于缓存项目的数据)获取数据(在本例中为位图),除非没有数据。在这种情况下,它会执行 DownloadImageTask,它将下载图像并将其存储在组件中。存储后,它会将图像放入 ImageView。
我的问题是,当 ImageViews 使用 ViewHolder 模式时,而不是“错误的方式”(每次调用 findViewById()),滚动列表将使错误的 ImageViews 获得下载的位图。这个 gif 显示了它的外观:
显然,我希望图像只出现在它们应该出现的地方。有什么好的方法可以使这项工作按预期进行吗?
最佳答案
我用Glide解决了这个问题。谢谢大家告诉我原来已经有这么美好的事情了!
(几乎)做同样的事情就像:
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
convertView = inflater.inflate(R.layout.item, null, false);
viewHolder = new ViewHolder();
viewHolder.imageView = (ImageView) convertView.findViewById(R.id.image);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
String url = components.get(position).getImageURL();
Glide.with(activity).load(url).crossFade().into(viewHolder.imageView);
return convertView;
}
关于java - 从服务器下载图片时出现 "Flickering images"带有 ViewHolder 模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32482625/