How to lazy load images in ListView in Android

2020-01-22 10:57发布

I am using a ListView to display some images and captions associated with those images. I am getting the images from the Internet. Is there a way to lazy load images so while the text displays, the UI is not blocked and images are displayed as they are downloaded?

The total number of images is not fixed.

30条回答
在下西门庆
2楼-- · 2020-01-22 11:10

All above code have their own worth but with my personal experience just give a try with Picasso.

Picasso is a library specifically for this purpose, in-fact it will manage cache and all other network operations automatically.You will have to add library in your project and just write a single line of code to load image from remote URL.

Please visit here : http://code.tutsplus.com/tutorials/android-sdk-working-with-picasso--cms-22149

查看更多
倾城 Initia
3楼-- · 2020-01-22 11:11

The way I do it is by launching a thread to download the images in the background and hand it a callback for each list item. When an image is finished downloading it calls the callback which updates the view for the list item.

This method doesn't work very well when you're recycling views however.

查看更多
看我几分像从前
4楼-- · 2020-01-22 11:11

I just want to add one more good example, XML Adapters. As it's is used by Google and I am also using the same logic to avoid an OutOfMemory error.

Basically this ImageDownloader is your answer (as it covers most of your requirements). Some you can also implement in that.

查看更多
唯我独甜
5楼-- · 2020-01-22 11:14

I have followed this Android Training and I think it does an excellent job at downloading images without blocking the main UI. It also handles caching and dealing with scrolling through many images: Loading Large Bitmaps Efficiently

查看更多
Emotional °昔
6楼-- · 2020-01-22 11:15

Check my fork of LazyList. Basically, I improve the LazyList by delaying the call of the ImageView and create two methods:

  1. When you need to put something like "Loading image..."
  2. When you need to show the downloaded image.

I also improved the ImageLoader by implementing a singleton in this object.

查看更多
一纸荒年 Trace。
7楼-- · 2020-01-22 11:18

URLImageViewHelper is an amazing library that helps you to do that.

查看更多
登录 后发表回答