Android: disable lazy list loading

In my Android application, I have a list containing 30 lines, and each line consists of several text views, one of which is spannable and sometimes contains a lot of formatted text and images.

Those images are downloaded from the network asynchronously: the placeholder is displayed until the image is downloaded, then replaced by the image.

Unfortunately, the list lines are displayed when I look through them. This makes everything very slow. In addition, these images are downloaded from the Internet again and again, whenever I look at a line.

How can I disable it so that ListView rows load when I view them? They should be downloaded once when I start my activity and never again.

Best wishes and thanks, Jan Oliver

+6
android listview
source share
3 answers

When you execute a lazy ListView, this is because you want to speed it up. Turning it off is not the best solution. So what you can do is implement the basic cache system so that you don't have to download and install ImageView again and again.

The easiest way to do this is to implement a HashMap with URLs as keys and bitmaps as values. Something like that:

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 these images are always the same, which means that every time you open the application the same images are downloaded, then you better save these images in the file system and use them from there.

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

Edit

As for your comment, I highly recommend that you watch the video that I posted. It's one hour, but it's really worth the effort. Using an adapter, checking if convertView is null is just an easy way to improve performance, although there are other methods that will further enhance your application. Also, if you had problems using this trick, it is due to the fact that you are probably using it incorrectly. Remember: even if you are not reinventing the ideas, you need to establish the meaning of each of the children's ideas, otherwise you will encounter some problems.

+5
source share

If you can, start with an array of images full of β€œplaceholder images,” and then load the images into the array that runs AsyncTask at creation time. When constructing strings, just reference the array. Thus, if he has a new image, he will download it if he does not receive a placeholder.

If you have a lot of data, it will be very slow and will be a harsh experience for the user.

+1
source share

Create a list of objects representing each row. Create a loader as a background thread that updates objects when loading data. Your list view will display data from objects.

(It is not a good idea if you have hundreds of lines and a huge amount of data in each line - in this case you should only load data in several lines of the current active line and have some kind of MRU cache).

0
source share

All Articles