Android: Smooth List Scrolling

If I had to compare iOS to Android and only list one major benefit iOS has over Android, it would be the scrolling list performance, hands down (and I’m not the only one, or two, or three to say this…).

Seeing as scrolling lists are essential to just about every major mobile app, it affects the end user the most. Android’s list view performance leads people to believe that Android is an inferior OS to iOS.

But as it turns out, with a handful of optimizations and some dedicated time, Android’s scrolling performance can be just as good as iOS’s performance.

You see, even though iOS does have a leg up in this department, iOS Developers still have to spent a good amount of time optimizing their scrolling lists to make them scroll smoothly. On Android, it just takes a bit more time.

When trying to improve Android’s scrolling performance with a list of times, it’s important to realize the following few points:

  1. Creating objects is very expensive
  2. Calling findViewById too much can be painful
  3. Drawing on the screen should be done only when necessary

Here’s a few quick optimizations you can make to get the most performance gains for the time spent.

The first optimization is the most common one: The View Holder. It reduces the amount of objects you have to create while scrolling and dramatically cuts down on some expensive UI calls. This optimization alone will net about 100 % increase in performance. A good example of the pattern in action can be found here.

One other less-used optimization is to only draw views when the user isn’t actually scrolling the list. Here’s the idea: If your list view item consists of a few images, some text, and a checkbox, does your user really need to see all of these views generated as they are scrolling by?

When the user is scrolling by, all of the work going into calculating detailed views goes to waste, and it kills the scrolling animation.

The user also doesn’t need to have such a detailed view of scrolling data, either, only the ability to determine where the user is currently in the list. For example, in a list sorted alphabetically, you know where you are by checking the text in the list.

So instead of drawing all of these useless views while scrolling, why not only draw the text on the view? By only drawing the most important views, your list view should see a huge bump in scrolling speed, making it look much, much smoother.

For the code below to work properly, you need to add a boolean value (isScrolling) to your adapter. Any time the scrollState isn’t zero (or scrolling), you set this value to true. Once the scrolling stops, you can tell the adapter to redraw the views with the details necessary.

listView.setOnScrollListener(new OnScrollListener() {
                     
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCoun) {
    public void onScrollStateChanged(AbsListView view, int scrollState) {
       if (scrollState != 0)
           listView.getAdapter()).isScrolling = true;
       else {
           listView.getAdapter()).isScrolling = false;
           listView.getAdapter()).notifyDataSetChanged();
       }
    }
});

After you add the above code in your listView.getView() method, you can use the newly added boolean value to determine if any performance-intensive views need to be drawn or not, like images or complex buttons.

The above two optimizations will give you a good jump in performance for a minimal amount of invested time.

Do you know of some other hidden optimization gems for Android’s List View performance?

Reference: Buttery Smooth List Scrolling in Android from our JCG partner Isaac Taylor at the Programming Mobile blog.

Related Whitepaper:

Rapid Android Development: Build Rich, Sensor-Based Applications with Processing

Create mobile apps for Android phones and tablets faster and more easily than you ever imagined

Use 'Processing', the free, award-winning, graphics-savvy language and development environment, to work with the touchscreens, hardware sensors, cameras, network transceivers, and other devices and software in the latest Android phones and tablets.

Get it Now!  

One Response to "Android: Smooth List Scrolling"

  1. afh says:

    Never knew about #2. Dig it.

Leave a Reply


+ 1 = two



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books