Saturday, 31 October 2015

RecyclerView and CardView on Android

If you are interested in building an Android app that makes use of lists to display data, Android Lollipop features two new widgets to make your life easier,RecyclerView and CardView. Using these widgets, it is very easy to give your app a look and feel that conforms to the guidelines mentioned in Google's material design specification.
To follow along, you should be using the latest version of Android Studio. You can get it from the Android Developer website.
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 and ImageViewfields are set to meaningful values, then this is how it would render on an Android device:



A Stand-alone Card

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 extends RecyclerView.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 Person objects, 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 the sizemethod 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 customViewHolder.
Override the onBindViewHolder to specify the contents of each item of theRecyclerView. This method is very similar to the getView method of a ListView'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:
Advertisement
When you run the RecyclerView example on an Android device, you should see something similar to the following result.




In this tutorial, you have learned how to use the CardView and RecyclerViewwidgets that were introduced in Android Lollipop. You have also seen examples of how to make use of these widgets in Material Design apps. Note that even though aRecyclerView can do almost everything a ListView can, for small datasets, using aListView is still preferable as it requires fewer lines of code.
You can refer to the Android Developers Reference for more information about theCardView and RecyclerView classes.

No comments:

Post a Comment