Sunlight 1.06.01 – RecyclerView vs. ListView

Another new feature introduced along Android L is the RecyclerView which ist basically a ListView on steroids. Much more powerful, but harder to tame at the same time.

[*Caution: Outdated content. My sunlight project has been discontinued. It refers to earlier versions of the v7 appcompat Library (21.0.3) and Android Studio (1.1).  I’ll keep the posts online for reference but with the current version of appcompat Library things may be different / easier today, so please refer to more up-to-date sources.]

This article is part of the Sunlight project and refers to the second half of Lesson 1 of Googles Udacity Course “Developing Android Apps”.

If you are an android novice and follow the Udacity course while reading my blogposts along to get the “L-update”, this whole thing might be a litte complex in first place. Many of the concepts used by RecyclerView are “more advanced” stuff that will be discussed later in the Udacity course in Lesson 5. I hope you’ll get along anyway. Else you might keep track with the basic course first and come back to this later.

Some basics

Dont’t want to re-describe the wheel, so here we got some references. A good blog post from Antonio Leva and again a video by slidenerd.

Replacing ListView by RecyclerView

We start by importing the library with this addition to the dependencies section of the app/build.gradle file:

And do a Gradle Sync afterwards.

Now in layout/fragment_main.xml we replace the ListView by and also rename the id from listview_forecast to recyclerview_forecast.

Next step is to modify



But sadly this will not work with our current adapter 🙁

New kind of adapter

RecyclerView depends on a different adapter type which also requires a so called “view holder”. This common pattern is introduced in chapter 5 of the Udacity course. In short: It is a class that holds references to the inflated layout of a view item. This was a best practice when applied to ListViews and is a requirement for the RecyclerView.

Let’s take a look at the adapter evolvement (in

The old ArrayAdapter:

The new view holder (to be defined within the PlaceholderFragment class):

And here comes the new ForecastAdapter (also within PlaceholderFragment class)

One thing is important: The mWeekForecast ArrayList has been a local variable in the onCreateView method. We have to move the declaration to make it a member variable of the PlaceholderFragment class:

Last thing is to correct the Adapter settings and add a layout manager to make the items show up as a linear list:

Now the code is complete. You may take a look at the complete code of MainActivity in my Github Repo.

Let’s run the code and see the result:

Hmm – you have to watch carefully. The text is shown, but in a very bright color. So we have to adjust the text color in layout/list_item_forecast.xml

Run again and see this:

Find the source code in my GitHub Repo.


Well – now with much effort we have a result that looks – hmm – exactly the same. What for? Here’s a funny comment from William Hatch to Antonio Leivas post describing the usage of RecyclerView that brings it to the point:

Yes. And no: There is much more about the RecyclerView that makes it preferable over ListView: It’s versatility to play with different layouts. Ability to transition in and out list elements and so much more. But that’s another story for another time…

For now we’ll continue with Lesson 2 “Connect Sunshine to the cloud” of the Udacity Course in the next post.

Leave a Reply

Your email address will not be published. Required fields are marked *