When i started making my first android app, i wanted to make an heterogenous list, i remember having a lot of app crashes and it was a real pain because i wanted to get the best way of doing it and that's why i will show you an easy way to get it.
I will be providing the building blocks for you to be able to create differents layouts using heterogenous ReyclerView
On my opinion one of the first things you need to start building your app is visualize how the app will look like after you have that figure out lets prepare out layout.
To prepare our app we will make 3 different layouts : main layout, vertical item and horizontal item
Lets start with the main layout
On the main layout we will be placing a RecyclerView and we will add an id on my case we have verticalRecyclerView. Always remember to add an id to your elements so you will be able to use them
now we will prepare our vertical items.
On my vertical item i have a linear layout that holds a TextView that will have the section number and the 2nd RecyclerView that will display the horizontal list
at last we will create the item that will be display on the horizontal list
On this layout we will incorporate what elements we want to be shown on within each element of out horizontal RecyclerView
When we want to work with a RecyclerView there are 2 concepts that we need to address: Adapter and ViewHolder.
The adapter is the piece that will connect our data to our RecyclerView and determine the ViewHolder which will need to be used to display that data and the ViewHolder will be the pattern that our RecyclerView will use to display said data
Let's start by creating our horizontal adapter and viewHolder
Now we have set the child adapter with its viewHolder. The first thing that we have is the oncreateViewHolder, in there we will be inflating our horizontal item layout and returning it so our View holder will know where is getting the meta data from.
then we have the function getItemCount this will define how many item will the recyclerview be displaying. on this function you can asing a list size or any int to define the amount of items.
Then we have the onBindViewHolder, this function will asing the information to each item you have selected within the viewHolder
Last but not least we have to call each element within out layout to be able to use it on the apdapter and for that we use the ViewHolder
Now that you know is the function of each element within our first adapter we will use the same knowledge to create our parent adapter that will call the horizontal Adapter
Now as u see the elements are the same the only difference is that now on our ViewHolder we need to add wich recyclerView will be our child after we define that, we will go to our onBindViewHolder and we will start by creating our layout manager in order for us to specify that all the objets will be display horizontaly we need to specify the orientacion within out LinearLayoutManager call
after we have our layoutManager we will apply it, thereforewe will call the apply funtion on our recyclerView and we will be setting up the layout manager and the adapter
Now that we have setup both adapters the onlything that we have left will be to call out first adapter from our activity class.
now we will be setting up out layoutManager and the adapter aswell after that you will be able to run the app without problems and will have a result similar to the following:
Do you want to check the project? Click Here