Pages

2.08.2014

Making an endless list load smoothly in Android

In my latest Android app endeavor, I needed to create an endless list.  There's no built in class that does this, so I had to create something that would approximate one.  There are plenty of tutorials on the basic ideas behind endless lists, but none of them worked very well for my application.

My app is a viewer for interfacelift.com, which provides high quality wallpaper downloads.  The app displays a list of wallpapers in descending chronological order.  The user can continue scrolling indefinitely (until the site runs out of wallpapers).   The standard way of approaching the infinitely scrolling list tends to be glitchy when the next set of wallpapers loads.  I've come up with a simple solution to solve the glitchy-ness problem.

When the app loads a set of wallpapers, it adds them to an ArrayList, which is displayed by the ListView.  However, the list adapter doesn't know about the new wallpapers until it is explicitly told that the data set has changed with a call to notifyDataSetChanged().  When this happens, the list adapter is forced to update all of the views.  In my app's case, issuing a new view sets the ImageView's drawable to a placeholder, which is almost immediately replaced with the real image.  This causes the entire list to "jerk" when the existing images are updated.


Continue reading to see code solution.


The fix for this is quite simple.  If you're already using the ViewHolder pattern, it's even easier.  Here's how you do it.  This is your ViewHolder class.  This can be nested somewhere or in it's own file.

public class ViewHolder{
    public ImageView previewImage;
    public TextView titleText;
    public int position = -1;
    public Handler handler;
}

Change the getView method inside the Adapter to look something like this:


The only thing we've really done is create an if statement that checks if the View will be rendered in the same place.  With that change in place, calling notifyDataSetChanged() on the adapter no longer makes the list look glitchy.

1 comment:

harmenijacquese said...

Wynn Casino & Hotel | DrmCD
This casino and 파주 출장마사지 hotel features a spa, an indoor 영주 출장마사지 pool, 원주 출장샵 and a Jacuzzi. It's 영주 출장마사지 a perfect getaway. Guests 여수 출장안마 can book a shuttle, pick up and dine online.