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:
link
And to make an example of a RecyclerView
, they recommended the following web page:
link
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:
mAdapter.notifyDataSetChanged();
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());
recyclerView.setLayoutManager(mLayoutManager);
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);
recyclerView.setLayoutManager(mLayoutManager);
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.