Android Core

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.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
afh
afh
9 years ago

Never knew about #2. Dig it.

Back to top button