Android RecyclerView with CardView Example


The RecyclerView class extends the ViewGroup class and implements ScrollingView interface RecyclerView is more advanced version of  Listview. It is a container used for displaying large amount of data sets that can be scrolled very efficiently by maintaining a limited number of views.

Read more About RecyclerView

  Add support library

The RecyclerView widget is a part of separate library. Add the following dependency in your Gradle build file to use recyclerview.

1. Open the build.gradle file for your app module.
2.  Add the support library to the dependencies section.




Add RecyclerView in your layout


1. First of all create one layout separate file for RecyclerView so you can manage all property RecyclerView  at one place.
include_recyclerview.xml


Include RecyclerView layout 




RecyclerView Item Layout using CardView

We need a separate layout for items inside RecyclerView.
create a file row_recylerview.xml



Create RecyclerView Adapter


Create a RecyclerViewAdapter.java class. This class extends RecyclerView.Adapter class and override its unimplemented methods. The onCreateViewHolder() methods inflates the row_recylerview.xml. In the onBindViewHolder() method each data items are set to each row.


Setup Adapter to RecyclerView

In MainActivity.java first we can init recyclerView  and setup the layout manager as you need i set the the LinearLayoutManager for listView and set the adapter to recyclerView  

Output:


2 comments: