Learn step by step to use RecyclerView on Android


I am trying to implement a RecyclerView from a horizontal list in an application that I am creating and although I have found several examples of use, in all, they only show the code without explaining in detail what it is for.

I have an example in my application that works correctly, but it is a copy / paste of an example and I want to learn how it is used without copying and pasting when I need it.

To clarify my doubts, I know that to use a RecyclerView you use a LayoutManager, an Adapter with ViewHolder and a DataSet class that is the object class that will be used to collect all the data that will be shown in the list, but I do not know so that the LayoutMaganer or the ViewHolder is used or how they work together.

It would be very helpful if you explained how the RecyclerView works and if it is very extensive, I ask you to leave a comment as a comment explaining my doubt.

asked by Vicky Vicent 01.04.2016 в 13:49

1 answer


After all the weekend thinking about the matter, I managed to launch a horizontal RecyclerView with a brief understanding so that each component is used.

Well, I have found this page that explains in detail to serve each element:


And to make an example of a RecyclerView , they recommended the following web page:


This page is in English, but it explains all the steps to follow to create a RecyclerView correctly and following all the steps I could understand so that everything is used. For those who do not understand English, I guarantee that using the translator will understand perfectly.

I also have to say that I had some bugs with the code shown on the AndroidHive page.

It turns out that in the method onCreate() of the file MainActivity.java the method prepareMovieData() is called last, when it would have to be called just after declaring the RecyclerView, that is, just after this line:

recyclerView = (RecyclerView) findViewById(R.id.recycler_view);

If this call is declared last, we will see an error that the object we have created (in the case of AndroidHive is the Movies class) is null, that is, it does not have any data.

On the other hand, at the end of the method prepareMovieData() the following line is declared:


To me personally, this line causes me a flaw in the application that causes the App to close, removing it from the code works perfectly for me. Actually, I do not know what this line is for, but I would appreciate it if anyone knows why, that I will edit this post explaining it.

Finally, I wanted to clarify that the intention of this post was to create a RecyclerView horizontally, so I leave the explanation below so that a RecyclerView is displayed horizontally or vertically.

In the onCreate() of the file MainActivity.java we add a LayoutManager to the RecyclerView with the following lines:

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());


This LayoutManager is responsible for telling the RecyclerView how the data we enter will be displayed, that is, whether the content will appear vertically, horizontally, in the form of a list, etc. So if we want our list to appear in horizontal we only have to edit the previous lines by the following:

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext(), LinearLayoutManager.HORIZONTAL, false);


If you want the list in vertical , we leave the lines as they are.

I hope it will work for you and thank you for the support.

answered by 04.04.2016 / 11:52