I haven't looked in details at the 3 other approaches but I know that a big difference between what I do in Shelves and what I've seen done in other places is how user interaction is handled. Shelves listens to scroll events on the ListView and tries to identify taps vs scrolls vs fling to always give priority to the UI. For instance, when you touch the screen to stop a fling, Shelves starts loading images, but does so only after a very short delay so that if you are touching the screen to fling some more, the fling animation won't stutter because of the extra work load. But that's details.
On Sat, Feb 13, 2010 at 8:01 PM, Carmen Delessio <carmendeles...@gmail.com> wrote: > Here are 4 references that I have found for lazy-loading images into a > listview in Android. > The idea is to display a placeholder image, get the actual image in the > background, update the ImageView in the list when the image is available. > > I've tried to do this in the simplest way possible using an AsyncTask in an > Adapter. > The outline of that approach is below. Is it flawed? > Is there an agreed approach to handling this common task? > > Thanks, > Carmen > > http://blog.jteam.nl/2009/09/17/exploring-the-world-of-android-part-2/ Tom > van Zummeren > http://evancharlton.com/thoughts/lazy-loading-images-in-a-listview/ Evan > Charlton > http://code.google.com/p/shelves/ from Romain Guy > http://github.com/commonsguy/cwac-thumbnail from Mark Murphy > > > Approach using AsyncTask within an Adapter > > Somewhere in the Adapter. > Add info about the URL for the image to fetch to the ImageView. > Execute an AsyncTask to get the image in the background and load the > ImageView. > AsyncTask uses the tag to fetch the image. > It uses the tag to find the correct view within the ListView. > ImageView imageViewByTag = (ImageView) myListView.findViewWithTag(tag); > ImageView is updated. > > Somewhere in the Adapter: > > myImageView.setTag("http://someimage.jpg"); // > try { > new GetImage().execute(myImageView); > } catch(RejectedExecutionException e) { } > > > // Asynctask > private class GetImageInBackground extends AsyncTask<ImageView , ImageView , > Long > { > Drawable drawable; > String tag; > > @Override > protected void onProgressUpdate (ImageView... params){ > if (drawable!=null){ > ImageView imageViewByTag = (ImageView) > myListView.findViewWithTag(tag); // find the ImageView > if (imageViewByTag != null) { > imageViewByTag.setImageDrawable(drawable); //update it > } > > } > } > > @Override > protected Long doInBackground(ImageView... params) { > tag = (String) params[0].getTag(); // get the tag from the > ImageView > > if (tag==null){ > drawable = getPlaceholder(); // get a default drawable > }else{ > > bm =getDrawablefromURL(String (tag); // a method to get > drawable from a URL > } > publishProgress(params[0]); > return null; > } > > } > > -- > Carmen > http://www.twitter.com/CarmenDelessio > http://www.talkingandroid.com > http://www.facebook.com/BFFPhoto > http://www.twitter.com/DroidDrop > > -- > You received this message because you are subscribed to the Google > Groups "Android Developers" group. > To post to this group, send email to android-developers@googlegroups.com > To unsubscribe from this group, send email to > android-developers+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/android-developers?hl=en -- Romain Guy Android framework engineer romain...@android.com Note: please don't send private questions to me, as I don't have time to provide private support. All such questions should be posted on public forums, where I and others can see and answer them -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en