A nested RecyclerView is an implementation of a RecyclerView within a RecyclerView. An example of such a layout can be seen in a variety of apps such as the Play store where the outer (parent) RecyclerView is of Vertical orientation whereas the inner (child) RecyclerViews are of horizontal orientations. A similar layout is developed here.
Step 1:Add the required dependencies
Add the following dependencies to build.gradle (:app) file.The first dependency corresponding to RecyclerView is mandatory and the second one is for CardView, thus it is optional depending upon the UI requirements. Since here CardView is used in the child RecyclerView layout, so the CardView dependency is added.
Note: After adding the dependencies, click on Sync now.
Note: For the correct versions of dependencies check here.
Step 2: Implement the parent RecyclerView
The parent RecyclerView is implemented in the activity_main file. Here, activity_main file only holds the parent RecyclerView, but it can be customized as per the requirements.
Step 3: Create item layout for the parent RecyclerView
Identify requirements for the parent layout and add Views corresponding to them in an XML layout file. Here, layout files are named as parent_item. In addition to the child RecyclerView, a TextView for the title is included.
Step 4: Create item layout for the child RecyclerView
The layout of the items of the child RecyclerView is created in an XML layout file named child_item. Child RecyclerView includes an ImageView and a TextView, both wrapped inside a CardView.
The following image is used as the resource of the ImageView, thus it is added in the drawable resouce folder.
Step 5: Create Item classes for each of the RecyclerViews.
A Java class is required for each RecyclerView wherein the constructor initializes the parameters of the item and the getter and setter methods are declared. Thus, create the following Java classes:
- ChildItem: This class will implement the constructor, getter, and setter methods for the Views of the child_item layout that we wish to set dynamically.
- ParentItem: This class will implement the constructor, getter, and setter methods for the Views of the parent_item layout that we wish set dynamically.
Step 6: Create an Adapter class for each of the RecyclerViews.
An adapter class is required to pass the data that is to be displayed in the views of RecyclerView.
An adapter class is needed for the ChildItem as well as ParentItem classes.
- ChildAdapter: This is the adapter class for holding the information that is to be displayed in the views of the ChildItem class.
- ParentAdapter: This is the adapter class for holding the information that is to be displayed in the views of the ParentItem class.
In the ParentItemAdapter class, an instance of the class is created
This class is used to pass Views between the parent RecyclerView and the child RecyclerView.
Step 7: Set the layout of parent RecyclerView and pass the arguments to both the RecyclerViews. The arguments to be passed to the RecyclerViews are given in the Java class that inflates the activity of the parent RecyclerView (here activity_main). So here the arguments will be passed in the MainActivity file.
Also, set the layout for the parent RecyclerView here itself.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.