At the time of writing, less than 2% of Android devices run Android Lollipop. However, thanks to the v7 Support Library, you can use the RecyclerView andCardView widgets on devices that run older versions of Android by adding the following lines to the dependencies section in your project's build.grade file:
CardView is a ViewGroup. Like any other ViewGroup, it can be added to yourActivity or Fragment using a layout XML file.
To create an empty CardView, you would have to add the following code to your layout XML as shown in the following snippet:
As a more realistic example, let us now create a LinearLayout and place aCardView inside it. The CardView could represent, for example, a person and contain the following:
  • TextView to display the name of the person
  • TextView to display the person's age
  • an ImageView to display the person's photo
This is what the XML would look like:
If this XML is used as the layout of an Activity, with the TextView andImageView fields are set to meaningful values, then this is how it would render on an Android device:
Using a RecyclerView instance is slightly more complicated. However, defining it in a layout XML file is quite simple. You can define it in a layout as follows:
To obtain a handle to it in your Activity, use the following snippet:
If you are sure that the size of the RecyclerView won't be changing, you can add the following to improve performance:
Unlike a ListView, a RecyclerView needs a LayoutManager to manage the positioning of its items. You could define your own LayoutManager by extending theRecyclerView.LayoutManager class. However, in most cases, you could simply use one of the predefined LayoutManager subclasses:
  • LinearLayoutManager
  • GridLayoutManager
  • StaggeredGridLayoutManager
In this tutorial, I am going to use a LinearLayoutManager. ThisLayoutManager subclass will, by default, make your RecyclerView look like aListView.
Just like a ListView, a RecyclerView needs an adapter to access its data. But before we create an adapter, let us create data that we can work with. Create a simple class to represent a person and then write a method to initialize a List ofPerson objects:
To create an adapter that a RecyclerView can use, you must extendRecyclerView.Adapter. This adapter follows the view holder design pattern, which means that it you to define a custom class that extendsRecyclerView.ViewHolder. This pattern minimizes the number of calls to the costly findViewById method.
Earlier in this tutorial, we already defined the XML layout for a CardView that represents a person. We are going to reuse that layout now. Inside the constructor of our custom ViewHolder, initialize the views that belong to the items of ourRecyclerView.
Next, add a constructor to the custom adapter so that it has a handle to the data that the RecyclerView displays. As our data is in the form of a List of Personobjects, use the following code:
RecyclerView.Adapter has three abstract methods that we must override. Let us start with the getItemCount method. This should return the number of items present in the data. As our data is in the form of a List, we only need to call thesize method on the List object:
Next, override the onCreateViewHolder method. As its name suggests, this method is called when the custom ViewHolder needs to be initialized. We specify the layout that each item of the RecyclerView should use. This is done by inflating the layout using LayoutInflater, passing the output to the constructor of the custom ViewHolder.
Override the onBindViewHolder to specify the contents of each item of theRecyclerView. This method is very similar to the getView method of aListView's adapter. In our example, here's where you have to set the values of the name, age, and photo fields of the CardView.
Finally, you need to override the onAttachedToRecyclerView method. For now, we can simply use the superclass's implementation of this method as shown below.
Now that the adapter is ready, add the following code to your Activity to initialize and use the adapter by calling the adapter's constructor and the RecyclerView'ssetAdapter method:
When you run the RecyclerView example on an Android device, you should see something similar to the following result.