Lazy Loading in Lists          Janusz Leidgens          @Killerdackel          Plattformmanager Android          Kupferwer...
Classic Lazy Loading Patternpublic View getView(int position, View convertView, ViewGroup parent) {    … setupView   image...
Problems●   Imageview redraw is triggered asynchronously●   Scrolling behavior suffers●   Gallery becomes unusable
Better solutionpublic View getView(int position, View convertView, ViewGroup parent) {    … setupView   image = getImage(u...
Advantages●   Listadapter chooses best moment to redraw●   Scrolling is smooth
Problems●   All cells on the screen are redrawn●   Special care needed to prevent multiple    downloads
Downloading Images●   Save image urls to queue●   Prevent double entries in queue●   Choose a device specific number of wo...
How to download one image?●   Check if image is in In-Memory-Cache → use●   Check if image is on disk → load to memory●   ...
Correct in memory Caching    ●    Classic caches use Softreferences    ●    Android frees Softreferences immediately    ● ...
Upcoming SlideShare
Loading in …5
×

Droidcon 2012: Lazy loading in lists

3,123 views
3,017 views

Published on

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.

Published in: Technology, Business
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,123
On SlideShare
0
From Embeds
0
Number of Embeds
1,100
Actions
Shares
0
Downloads
27
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

Droidcon 2012: Lazy loading in lists

  1. 1. Lazy Loading in Lists Janusz Leidgens @Killerdackel Plattformmanager Android Kupferwerk GmbH
  2. 2. Classic Lazy Loading Patternpublic View getView(int position, View convertView, ViewGroup parent) { … setupView image = loadImage(url); if(image != null) { imageView.setImageBitmap(image); } else { Task loadImage = new Task() { protected Void doInBackground(Void... params) { image = loadImageFromNetwork(url) } protected void onPostExecute(Void result) { if(image != null && imageView.getTag().equals(url)) { imageView.setImageBitmap(image) } … setup no image loaded view } } imageView.setTag(url); loadImage.execute(); }
  3. 3. Problems● Imageview redraw is triggered asynchronously● Scrolling behavior suffers● Gallery becomes unusable
  4. 4. Better solutionpublic View getView(int position, View convertView, ViewGroup parent) { … setupView image = getImage(url); if(image != null) { imageView.setImageBitmap(image); } else { Task loadImage = new Task() { protected Void doInBackground(Void... params) { image = loadImageFromNetwork(url) } protected void onPostExecute(Void result) { if(image != null) { adapter.notifydatasetchanged(); } … setup no image loaded view } } loadImage.execute(); }
  5. 5. Advantages● Listadapter chooses best moment to redraw● Scrolling is smooth
  6. 6. Problems● All cells on the screen are redrawn● Special care needed to prevent multiple downloads
  7. 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. 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. 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; }}

×