Android Working with Recycler View

Android RecyclerView is more advanced version of ListView with improved performance and other benefits. Using RecyclerView and CardView together, both lists and grids can be created very easily. Here is the complete information about RecyclerView and other examples.

In this tutorial we are going to learn how to render a simple RecyclerView with a custom layout. We’ll also learn writing a adapter class, adding list divider and row click listener. The recycler view we are going to design contains list of movies displaying the title, genre and year of release.

Below is the RecyclerView widget with necessary attributes.


Before you start, make sure that you updated your Android Studio to latest version. Currently my Android Studio is updated to Android Studio 2.0 Preview 6.

1. Creating New Project

1. In Android Studio, go to File ⇒ New Project and fill all the details required to create a new project. When it prompts to select a default activity, select Blank Activity and proceed.

2. Open build.gradle and add recycler view dependency. and rebuild the project.


3. With the latest version of build tools, Android Studio is creating two layout files for each activity. For main activity, it created activity_main.xml (contains CoordinatorLayout and AppBarLayout) andcontent_main.xml (for the actual content). Open content_main.xml and the recycler view widget.


4. Open colors.xml located under res ⇒ values and add below colors.


2. Writing the Adapter Class

After adding the RecyclerView widget, let’s start writing the adapter class to render the data. The RecyclerView adapter is same as ListView but the override methods are different.

5. Create a class named and declare title, genre and year. Also add the getter/setter methods to each variable.


6. Create an layout xml named movie_list_row.xml with the below code. This layout file renders a single row in recycler view by displaying movie name, genre and year of release.


7. Now create a class named and add the below code. HereonCreateViewHolder() method inflates movie_list_row.xml. In onBindViewHolder() method the appropriate movie data (title, genre and year) set to each row.


8. Now open and do the below changes. Here prepareMovieData() method adds sample data to list view.


Now if you run the app, you can see the movies displayed in a list manner.


3. Adding RecyclerView Divider / Separator

RecyclerView doesn’t have any divider related parameters to display the divider. Instead you need to extend a class from ItemDecoration and use addItemDecoration() method to display the divider.

9. Create a class named and paste the below code.


10. Open and set the item decoration using addItemDecoration() method before setting the adapter.


Now if you run the app, you should see a divider line separating each row.


4. Adding RecyclerView Item Click Listener

RecyclerView doesn’t have OnItemClickListener method too to identify item click. You need to write your own class extending RecyclerView.OnItemTouchListener.

11. Open and add the below RecyclerTouchListener class along withClickListener interface.


Finally add the recycler view item click listener as mentioned below.


Run the app and verify the item click. You should able to see a toast message upon clicking on a row. You can also notice that a background ripple effect when testing on lollipop device.


Final Code

Below is the complete code of my


Related Posts
Leave a reply