Using RecyclerView in Android

Chidume Kenneth
Dec 5, 2020 · 5 min read
Image by Christian Wiediger on Unsplash


Recyclerview class extends the ViewGroup class and implements scrollingView interface. It is an advanced version of the listview. It is an improvement to both listview and gridview and can be found in the latest v-7 support packages . This improvement is achieved by recycling the views which are out of the visibility of the user and as such minimizing memory usage. For example, if a user scrolled down in an app’s screen with list of items rendered using recyclerview to a position where items 4 and 5 are the only visible items on the user’s screen at that point in time; items 1,2 and 3 would be cleared from the memory to reduce memory consumption. The Recyclerview widget is usually used when one has data collections whose elements change at runtime based on user action or network events. A recyclerview accesses its data using an adapter and positions its items using a layout manager


First add the dependency identifier for this library in the Gradle script’s build.gradle(module:app) by copying and pasting the below dependency identifier inside the dependencies opening and closing curly bracket

dependencies {
implementation ''

Or you could follow the following steps:

In android studio go to File — Project structure. In the project structure window ,click on the “app” option and switch to the Dependencies tab. Then click on the “+” button at the right or bottom side of the screen, choose “Library dependency” option,and type in the complete dependency identifier as above and then click on search. when the search is done click on “ok” and then on “apply” to save changes.

Create an XML layout for the view you wish to use inside the recyclerview, most people prefer card view for this. Card view is a type of frame layout that lets you display information on a virtual card. They can have rounded corners and shadows to make them look as though they are positioned above their background. To use this view you have to also add dependency identifier for it in the gradle the same way we did for the recyclerview.

dependencies {
implementation ''

Unlike a listview which uses built in adapters that comes with android, recyclerview requires you to write an adapter for it.

(i) Create an adapter class and extend the Recyclerview.Adapter class

The adapter has two main jobs: to create each of the views that are visible within the recyclerview, and to bind each view to a piece of data. You create a recyclerview adapter by extending the RecyclerView.Adapter class and overriding various methods. Example if I want to create an adapter named NotesOverviewAdapter for my recyclerview, I would start by making my NotesOverviewAdapter extend the Recyclerview.Adapter class.

1.class NotesOverviewAdapter extends 2.RecyclerView.Adapter<NotesOverviewAdapter.ViewHolder> {3.public static class ViewHolder extends RecyclerView.ViewHolder {}

The code inside the angle bracket implies that NotesOverviewAdapter.ViewHolder is defined as an inner class in this adapter class, which is the ViewHolder class defined in line 3, we will come back to it later and complete it.

(ii) Define a constructor for the class

Next, you’ll tell the adapter what sort of data it should use by adding a constructor.

1.class NotesOverviewAdapter extends 2.RecyclerView.Adapter<NotesOverviewAdapter.ViewHolder> {3. public static class ViewHolder extends RecyclerView.ViewHolder {}4. public NotesOverviewAdapter(List<String> subTopic){
5. this.subTopic = subTopic;

In this case I want the adapter to take a List of element type String called subtopic, and save the List as a private variable in line 5.

(iii) Define the ViewHolder inner class

This is where you define the type of view or views the recyclerview should use for each data item it is given. You can think of it as holder for the recyclerview’s view. It also includes information about each view’s position in the recyclerview. In my case I used a cardView and created the XML layout for it before now.

public static class ViewHolder extends RecyclerView.ViewHolder {
private CardView cardView;
public ViewHolder(CardView v) {
cardView = v;

(iv) Override the adapter’s methods


This is where you tell the adapter the number of data items it is working with. It returns an int value the number of data items. In my case I returned the List size because the List contains all the data that the adapter needs, so its size will be the number of data items the adapter will be working with.

public int getItemCount() {
return subTopic.size();


This method gets called when the recyclerview requires a new view holder. It is called repeatedly (depending on the number returned by the method getItemCount()) when the recycler view is first constructed to build the set of view holders to be displayed on the screen. This method takes two parameters: a ViewGroup parent object (the recycler view itself) and an int parameter called ViewType which is used if you want to display different items in the list unlike in my case which is just one, the card view. It returns a view holder object.

@Override1.public ViewHolder onCreateViewHolder( ViewGroup parent, int 2.viewType) {
3. CardView cv = (CardView) 4.LayoutInflater.from(parent.getContext()).inflate(R.layout.card_vie5.w, parent, false);
6. return new ViewHolder(cv);

Line 3–5 creates the CardView from the XML layout I created earlier , which I called card_view. Then in line 6 I instantiated and returned the ViewHolder’s constructor, I defined in the ViewHolder inner class.


Having created the number of view holders the recycler view needs in the onCreateViewHolder () method, it is now time to get the adapter populate the view (card view) created with data. You do this by implementing the adapter’s onBindViewHolder () method. This method gets called whenever the recycler view needs to display data in a view holder. It takes two parameters: the view holder the data needs to bound to, and the position in the data set of the data that needs to be bound.

public void onBindViewHolder(ViewHolder holder, final int position) {
final CardView cardView = holder.cardView;
TextView text1 = (TextView)cardView.findViewById(;



As we can see, recycler view is very simple to use but takes more time to implement its adapter unlike the list view whose adapter is a couple of codes long. Also it was created as an improvement to listview. For this reason it is a more flexible control for handling list data, especially when you don’t know exactly the size of your data.

The Startup

Get smarter at building your thing. Join The Startup’s +788K followers.

Sign up for Top 10 Stories

By The Startup

Get smarter at building your thing. Subscribe to receive The Startup's top 10 most read stories — delivered straight into your inbox, once a week. Take a look.

By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more information about our privacy practices.

Check your inbox
Medium sent you an email at to complete your subscription.

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Chidume Kenneth

Written by

Am from Nigeria

The Startup

Get smarter at building your thing. Follow to join The Startup’s +8 million monthly readers & +788K followers.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store