Java 列表视图上的异步图像加载器 [Android]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3178883/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:08:07  来源:igfitidea点击:

Asynchronous image loader on listview [Android]

javaandroidlistviewandroid-asynctaskimageview

提问by thpoul

I'm having problems implementing an asynchronous image loader to the following code. I read some posts around the web about it and I think I understand the logic behind it, but I seem to fail in implementing it.

我在为以下代码实现异步图像加载器时遇到问题。我在网上阅读了一些关于它的帖子,我想我理解它背后的逻辑,但我似乎没有实现它。

The code bellow is what I use to simply load the images in my listview.

下面的代码是我用来简单地在我的列表视图中加载图像的代码。

public class MyCustomAdapter extends ArrayAdapter<RSSItem> {
   Bitmap bm;

   public MyCustomAdapter(Context context, int textViewResourceId, List<RSSItem> list) {
      super(context, textViewResourceId, list); 
   }

   @Override
   public View getView(int position, View convertView, ViewGroup parent) {
      // TODO Auto-generated method stub
      BitmapFactory.Options bmOptions;
      bmOptions = new BitmapFactory.Options();
      bmOptions.inSampleSize = 1;
      bm = LoadImage(myRssFeed.getList().get(position).getDescription(), bmOptions);

      View row = convertView;

      if(row == null) {
         LayoutInflater inflater = getLayoutInflater();
         row = inflater.inflate(R.layout.rsslist, parent, false); 
      }

      TextView listTitle = (TextView)row.findViewById(R.id.listtitle);
      listTitle.setText(myRssFeed.getList().get(position).getTitle());
      ImageView listDescription = (ImageView)row.findViewById(R.id.listdescription);
      listDescription.setImageBitmap(bm);
      TextView listPubdate = (TextView)row.findViewById(R.id.listpubdate);
      listPubdate.setText(myRssFeed.getList().get(position).getPubdate());

      return row;
   }
}

采纳答案by Fedor

You may use my sample code as reference Lazy load of images in ListView

您可以使用我的示例代码作为参考在 ListView 中延迟加载图像

回答by androider

On solution would be to populate a class variable within your adapter, say, an ArrayList with the references all the "ImageView listDescription"

解决方案是在您的适配器中填充一个类变量,例如,一个 ArrayList,其中包含所有“ImageView listDescription”的引用

ArrayList<ImageView> allImageViews = new ArrayList<ImageView>();    
    ...

    public View getView(int position, View convertView, ViewGroup parent){
       ...
       ImageView listDescription=(ImageView)row.findViewById(R.id.listdescription);
       allImageViews.add(listDescription);
       ...
    }

    private class ImageDownLoader extends AsyncTask<ArrayList, Void, Void>{
       doInBackground(){
         for(ImageView imageView: allImageViews){
         BitmapFactory.Options bmOptions;
         bmOptions = new BitmapFactory.Options();
         bmOptions.inSampleSize = 1;
         bm = LoadImage(imageNameOrWhatever, bmOptions);
         imageView.setImageBitmap(bm);
       } 
    }

Then use an AsyncTask that goes through each ImageView, retrieves the associated Image and removes the ImageView from the ArrayList. It will download one at a time in the background while your gui will still respond.

然后使用 AsyncTask 遍历每个 ImageView,检索关联的 Image 并从 ArrayList 中删除 ImageView。它会在后台一次下载一个,而您的 gui 仍会响应。

回答by alexandreferreira

Have you looked SmartImageView? http://loopj.com/android-smart-image-view/

你看过 SmartImageView 吗? http://loopj.com/android-smart-image-view/

It's very simple library to load images asynchronously (:

异步加载图像是一个非常简单的库(:

some Features of this library

这个库的一些特点

Drop-in replacement for ImageView Load images from a URL Load images from the phone's contact address book Asynchronous loading of images, loading happens outside the UI thread Images are cached to memory and to disk for super fast loading SmartImage class is easily extendable to load from other sources

替代 ImageView 从 URL 加载图像 从手机的联系人通讯录加载图像 异步加载图像,加载发生在 UI 线程之外 图像缓存到内存和磁盘以实现超快速加载 SmartImage 类可以轻松扩展以从中加载其他来源