Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Turn off lazy loading of listview

In my Android App I have a listview containing 30 rows, and each row consists of several textviews of which one is spannable and sometimes contains a lot of formatted text and images.

Those images are loaded from the web asynchroneously: A placeholder is displayed until the image has been downloaded is then replaced by the image.

Unfortunately, the rows of the listview are loaded when I scroll over them. This makes the whole thing very slow. Also, those images are loaded from the web again and again, whenever I scroll over the row.

How can I turn it off, that the ListView rows are loaded when I scroll over them? They should be loaded once when I start the activity and never again.

Best regards and thanks in advance, Jan Oliver

like image 530
janoliver Avatar asked Nov 01 '10 22:11

janoliver


2 Answers

When you do a lazy-loading ListView, is because you want to speed it up your app. Turn it off is not the best solution. So, what you can do is implementing a basic cache system in order to avoid downloading and setting the ImageView again and again.

The easiest way to do so is implementing a HashMap with URLs as keys and Bitmaps as values. Something like this:

Map cache = new HashMap();
// then, on your lazy loader
Bitmap image = cache.get(urlOfTheImage);
if( image == null ){
    // download and decode the image as normal,
    // then assign the decoded bitmap to
    // the 'image' variable
    cache.put(image);
}
imageView.setImageBitmap(image);

If those images will be the same always, meaning that each time you open the app the same images will be downloaded, then you better save those images in the filesystem and use them from there.

On the other hand, if the images tend to change, you could implement some interesting stuff: use SoftReferences. There's an explanation in this video. This can also be used if you are loading images from the filesystem.

Edit

With regards to your comment, I highly recommend you watching the video I posted. It's one hour long, but it really worths the effort. When using an adapter, checking if the convertView is null is just a simple way to improve performance, though there are some other techniques that will improve your app even more. Also, if you had have problems while using that trick, is because you are probably implementing it the wrong way. Remember: even if you don't re-inflate the views, you do have to set the value of each one of the children views, otherwise you will experience some problems.

like image 137
Cristian Avatar answered Nov 03 '22 09:11

Cristian


If you can, start with an Image Array full of the "placeholder images", then download the images in to an Array firing an AsyncTask during on Create. During row view building just refer to the array. That way if it has the new image it will load it, if not it will get the placeholder.

If you have a lot of data its gonna get real slow and be a crappy expirience for the user.

like image 44
blindstuff Avatar answered Nov 03 '22 09:11

blindstuff