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.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Android: Smooth List Scrolling"

  1. afh says:

    Never knew about #2. Dig it.

Leave a Reply


5 − = four



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.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close