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 and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
- How to Create RecyclerView with Multiple ViewType in Android?
- Android | RecyclerView
- Android | Horizontal RecyclerView with Examples
- How to add Bullet list in a RecyclerView in Android?
- How to insert Slide From Bottom animation in RecyclerView in Android
- How to populate RecyclerView with Firebase data using FirebaseUI in Android Studio
- Android | How to Create/Start a New Project in Android Studio?
- Android | AdMob Banner Ads for Android Studio
- Android | Android Application File Structure
- Android | AdMob Interstitial Ads for Android Studio
- Android | Running your first Android app
- Android | How to add Radio Buttons in an Android Application?
- Android | How to Request permissions in Android Application?
- Difference between Android 1.1 and Android 4.0.1
- Difference between Android 1.1 and Android 2.3.4
- Difference between Android 1.0 and Android 1.1
- Difference between Android 1.0 and Android 7.1
- Difference between Android 1.0 and Android 2.3
- Difference between Android 1.0 and Android 2.2
- Difference between Android 1.0 and Android 1.6
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.