RecyclerView forms a very crucial part of the UI in Android App development. It is especially important to optimize memory consumption during the display of a long list of items. A RecylerView inflates a customized list of items. This list can have either all similar layouts or multiple distinct layouts. Here, such a RecyclerView with multiple ViewTypes is developed. Following is an example of Android RecyclerView with multiple views.
For more up-to-date versions of the dependencies, click here. While the first dependency is mandatory, the second one is optional depending upon the UI requirements. Click on Sync Now and proceed.
Step 2: Implement RecyclerView in activity_main.xml
Create a layout that holds the main RecyclerView. Here, it has been created in the activity_main.xml file. The given layout contains merely a Welcome TextView and a RecyclerView, however, it can be customized as per the requirements. Code for activity_main.xml is given below.
Step 3: Add the required drawable file
Before proceeding further, make sure all the necessary drawable resources are added under the drawable resource directory. In this tutorial, only the following icon has been used:
Step 4: Create all the item layouts
Identify all the different layouts the RecyclerView is required to hold and implement them all in separate XML files under the layout resource directory. Here, two distinct layouts have been created. The first one is implemented in layout_one.xml while the second one in layout_two.xml. The first layout consists of just a TextView wrapped inside a CardView. The following is its implementation.
The second item holds three elements wrapped inside a CardView.
- An ImageView
- A TextView
- Another TextView with a relatively smaller font size
Step 5: Create an Item class
Create a Java class that holds the public constructors corresponding to each layout. Thus, two constructors have been created here in the ItemClass.java file. In addition to constructors, the getter and setter methods too are declared here. This is done in order to safely access the private variables of the ItemClass outside of it. This is how the ItemClass.java looks for the layouts created above:
Step 6: Create the Adapter class
Create an adapter class to display the contents of the RecyclerView. In the Adapter class for multiple ViewType RecyclerViews, the following method is overridden in addition to the conventional
getItemViewType() method is solely responsible for choosing the layout corresponding to each item. Apart from adding an extra method, the other changes include defining a specific ViewHolder class for each of the item layouts. Follow the code given for a deeper understanding.
Step 7: Complete the MainActivity.java file
Following are the important tasks to be implemented in the MainActivity.java file.
- Set the content view as the XML activity wherein the main RecyclerView has been implemented, here activity_main.xml.
- Set the layout for the RecyclerView.
- Pass arguments to the RecyclerView.
- Set the Adapter.
Output: Run on Emulator
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 a nested RecyclerView in Android
- 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
- RecyclerView in Android with Example
- Pull to Refresh with RecyclerView in Android with Example
- RecyclerView as Staggered Grid in Android With Example
- RecyclerView using GridLayoutManager in Android With Example
- Android | How to Create/Start a New Project in Android Studio?
- Android | Creating multiple Screen app
- 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
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.