I am trying to implement a RecyclerView of a horizontal list in an application that I am creating and although I have found several examples of use, in all of them, they only show the code without explaining in detail what each thing is for.
I have an example in my app that works fine, but it's a copy/paste of an example and I want to learn how to use it without copying and pasting as needed.
To clarify my doubts, I know that to use a RecyclerView you use a LayoutManager, an Adapter with a ViewHolder and a DataSet class which is the object class that will be used to collect all the data that will be displayed in the list, but I don't know what it is for does not use the LayoutMaganer nor the ViewHolder nor how they work together.
It would be very helpful for you to explain to me how the RecyclerView works and if it is very long, I ask you to leave a link as a comment explaining my doubt.
After all the weekend thinking about it, I have managed to start a RecyclerView horizontally, briefly understanding what each component is used for.
Well, I found this page that explains in detail what each element is for:
https://erikcaffrey.github.io/2015/10/05/recyclerview/
And to make an example of a
RecyclerView
, they recommended the following web page:http://www.androidhive.info/2016/01/android-working-with-recycler-view/
This page is in English, but it explains all the steps to follow to create a
RecyclerView
correctly and by following all the steps I could understand what each thing is used for. For those who do not understand English, I guarantee that using the translator you will understand it 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
onCreate()
file method the methodMainActivity.java
is calledprepareMovieData()
last, when it should be called right after declaring the RecyclerView, that is, right after this line:If we declare this call last, an error will appear stating that the Object we have created (in the case of AndroidHive it is the Movies class) is null, that is, it has no data.
On the other hand, at the end of the method
prepareMovieData()
the following line is declared:For me personally, this line causes a failure in the application that causes the App to close, removing it from the code works perfectly for me. Really, I don't know what this line is for, but I would appreciate it if someone knows why, to edit this entry explaining it.
Finally, I wanted to clarify that the intention of this post was to create a
RecyclerView
horizontally, so I leave below the explanation for a toRecyclerView
be displayed horizontally or vertically.In the
onCreate()
file methodMainActivity.java
we add a LayoutManager to the RecyclerView with the following lines:This LayoutManager is in charge of telling the RecyclerView how the data we enter will be displayed, that is, if the content will appear vertically, horizontally, in the form of a list, etc. So if we want our list to appear horizontally , we only have to edit the previous lines by the following:
In case you want the list vertically , we leave the lines as they are.
I hope you find it useful and thanks for the support.