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

问题描述:

我在执行异步图像加载器到下面的代码时出现问题。我在网上阅读了一些关于它的帖子,我想我理解它背后的逻辑,但是我似乎无法实现它。列表视图上的异步图像加载器[Android]

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

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; 
    } 
} 

你可以用我的示例代码作为参考Lazy load of images in ListView

+0

哦,这真的很有帮助!这次真是万分感谢。我马上检查一下。 – thpoul 2010-07-05 18:00:18

+0

当我在编译器上运行它时,你的代码工作得非常好,并且完全符合我的要求。不幸。我无法在我的rss阅读器项目上实现它。我真的很感谢在这方面的一手牌。先谢谢你。 – thpoul 2010-07-05 19:35:59

+0

您可以照原样使用LazyAdapter。只需传递一组网址即可。并在getView R.layout.item中指定R.layout.rsslist。应该在那之后工作。 – Fedor 2010-07-05 23:20:05

在解决办法是您的适配器内填充类变量,比方说,与引用一个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); 
     } 
    } 

然后使用通过每个ImageView的去的的AsyncTask,检索关联的图像并从ArrayList中移除ImageView。它会在后台一次下载一个,而你的GUI仍然会响应。

+0

感谢您花时间回复!我已经尝试过,但是我一定在做错事,或者错过了一些事情,但我一直没能实现它。 – thpoul 2010-07-05 18:02:21

你有没有看SmartImageView? http://loopj.com/android-smart-image-view/

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

这个库的一些特点

下拉更换为ImageView的 加载图像从手机的联系人地址簿中的URL 加载图像 异步加载图像,加载发生在UI线程外 图像缓存到内存和磁盘以实现超快速加载 SmartImage类可轻松扩展以从其他来源加载

+0

欢迎来到堆栈溢出。请总结你答案中的链接;这样,如果链接失效,答案不会完全无用。 – michaelb958 2013-07-27 12:05:16

+0

谢谢你!对于那个很抱歉。 – alexandreferreira 2013-08-05 17:26:30