RecyclerView using CardView

In today's article, I will show you how to create RecyclerView using CardView and how to hide the toolbar when scrolling down.

Android RecyclerView and Android CardView was introduced with Material Design in Android Lollipop.

Android RecyclerView is a more advanced, powerful, and flexible version of the ListView. Android RecyclerView is similar to ListView except it forces us to use RecyclerView. ViewHolder class to hold the elements which is not a compulsion in ListView.

The CardView widget can be used to create simple cards. It can also support rounded edges and elevation. In this tutorial we are going to create an Android app with basic RecyclerView and CardView widgets.

Create a Android Project

Open Android Studio and create a new project.
The first thing you have to do is to go to build.gradle and add the following lines inside the dependencies tag.

Before implementing RecyclerView in Activity, we need to create an adapter to allow RecyclerView to send data to individual list items.

The Adapter acts as a bridge between an AdapterView and the underlying data for that view. The Adapter provides access to the data items. The Adapter is also responsible for making a View for each item in the data set.

Now, to hide the toolbar when scrolling down, we have to tell the RecyclerView to use a MyScrollListener implementation, which must be implemented in onMoved(). Add the following code to MainActivity after expanding RecyclerView.

In this segment, we monitor the current toolbar position by using the toolbarOffset variable and decide whether to hide the toolbar or not by calling the onMoved()method. onMoved() is an abstract method that must be overridden when MyScrollListener is implemented.