
The final step is to populate the RecyclerView with the data. You can use your images of choice as well.

The images referenced above, are generated by default when starting a project. Kenya", "This is a mountain in Kenya 8", null, HasImage.FALSE) Kenya", "This is a mountain in Kenya 7", null, HasImage.FALSE), Kenya", "This is a mountain in Kenya 6", R.drawable.ic_launcher_foreground, Kenya", "This is a mountain in Kenya 5", null, HasImage.FALSE), Kenya", "This is a mountain in Kenya 4", R.drawable.ic_launcher_background, Kenya", "This is a mountain in Kenya 3", null, HasImage.FALSE), Kenya", "This is a mountain in Kenya 2", null, HasImage.FALSE), Kenya", "This is a mountain in Kenya 1", null, HasImage.FALSE), In the activity_main.xml file, add the following code:įun getLandmarks(): ArrayList = arrayListOf( The more the layouts, the more the viewHolders required. Note: You can create as many layouts as you wish, based on the use case. Unlike the previous layout, the one above does not contain an image. The code above generates a cardView with an image and two textViews, one for the title and one for the description.Ĭreate a layout file named landmark_without_image.xml and paste the following code: To begin with, create a layout file named landmark_with_image.xml and paste the following code: This is the main goal of using multiple viewHolders where each corresponds to a single layout. Nevertheless, we can use more than one row item for the same RecyclerView. Usually, we would use a single row item for each RecyclerView. “Row” items are the layout files that form a unit item in the RecyclerView. Getting startedĬreate an empty project in Android Studio and give it a name of your choice. The landmark item can contain an image or not. To demonstrate how we can use multiple ViewHolder classes, we will create a simple application that displays a list of landmarks. Manage RecyclerView callback methods and their interactions.Implement two-typed ViewHolders in a single adapter.Understand why we need more than one ViewHolder class.


They allow us to pass different data objects during the RecyclerView callbacks. This is where multiple ViewHolders come in. Have you ever asked yourself how you can employ different types of data items in the same RecyclerView while maintaining a seamless experience? Normally, developers use a single type of item to populate data in the RecyclerView. In the imperative programming paradigm in Android, a RecyclerView is a widget used to display scrollable items.
