There is nearly no app not using a ListView, either to show a long list of items that would not fit on a single screen, or to display information that was downloaded from a backend and does not have a fixed, or known item count. To enable a ListView to contain an unlimited number of items, the contained views have to be created on the fly while the user scrolls through the list. A list that fails the expectations of the user in scrolling speed and smoothness tends to appear broken and let users turn away from an app. Since all network, or persistent memory calls slow down the item creation all work that will cause a ListView to stutter has to be moved to a background thread. If new information is available, or an image is loaded the list has to be updated. All this needs to happen without consuming to much resources and without noticeable interruptions for the user. This process is not trivial in Android and many examples as well as tutorials available in the Internet still show wrong solutions for this task. This talk will show the best known way to lazy load images, from the network and the file system, into a ListView and how to avoid the pitfalls of the Android ListView-API that cause a bad user experience in a lot of current Android Applications. After outlining a solution for the standard case the speaker will try to answer as much questions about specific ListView problems as possible in an extensive Q&A part.
5. Advantages
● Listadapter chooses best moment to redraw
● Scrolling is smooth
6. Problems
● All cells on the screen are redrawn
● Special care needed to prevent multiple
downloads
7. Downloading Images
● Save image urls to queue
● Prevent double entries in queue
● Choose a device specific number of workers
● Download images as long queue is not empty
8. How to download one image?
● Check if image is in In-Memory-Cache → use
● Check if image is on disk → load to memory
● Load image from internet → save to disk
9. Correct in memory Caching
● Classic caches use Softreferences
● Android frees Softreferences immediately
● LRUCache (Support Package)
public class BitmapCache extends LruCache<String, Bitmap> {
private static final int FIRST_HONEYCOMB_API_INT = 11;
public BitmapCache(int maxSize) {
super(maxSize);
}
@Override
protected int sizeOf(String key, Bitmap value) {
if (Build.VERSION.SDK_INT > FIRST_HONEYCOMB_API_INT) {
return value.getByteCount();
}
return value.getHeight() * value.getWidth() * 4;
}
}