RecyclerView Multiple View Types in Android with Example
Last Updated :
20 Dec, 2021
We've all used RecyclerView in Android to display a list in our applications. RecyclerView is used to display emails in the Gmail app, feeds on Facebook and Instagram, and messages in WhatsApp, among other things. However, as being an Android Developer you would have come across a fact that when we define a view and the list items are only displayed according to that view. But what if you want to add different view types to the RecyclerView? You can do so by adding Multiple Views to the same RecyclerView. This article will teach you how to use Multiple Views in RecyclerView.
Multiple Views in RecyclerView
A RecyclerView can contain multiple views. The feed page on Facebook, for example, is an example of Multiple View. In the same RecyclerView, you can add an image, a video, text, or a combination of all three. Chatting on WhatsApp is another example. We can think of the message we send as being placed in a RecyclerView. As a result, when you send a message, it appears on the right side of the screen. However, if someone on the other side sends a message, it will appear on the left-hand side. As a result, the view type has changed. This is how we will learn how to do it in this blog. We will create a RecyclerView and add two different views to it. You can create more view types according to your use case. We start by starting AndroidStudio and creating a new project:
Example
Creating Views for the project
In our project, we are going to use two views i.e. item_view_1.xml and item_view_2.xml
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0091ea"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:gravity="center_vertical"
android:text="View 1 Geeks for Geeks"
android:textColor="#FFFFFF"
android:textSize="24sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/link_on"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00bfa5"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="48dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:text="View 2 Geeks for Geeks"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/someImageView"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="@drawable/link_off"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Data type
A data class is required to store the data that will be displayed on the views. Make a data class called Data and add the following code: The viewType indicates which view is being used, either item view 1 or item view 2, and textData stores the data that will be displayed on the TextView.
Kotlin
data class Data(val theView: Int, val theText: String)
RecyclerView Adapter
Now, for the RecyclerView, we must create an Adapter class that will store all of the RecyclerView logic, such as when to display which view. Make a RecyclerViewAdapter Adapter class. In the RecyclerViewAdapter class, if the desired view is item view 1, the content of item view 1 will be displayed; otherwise, the content of item view 2 will be displayed.
Kotlin
class gfgViewAdapter(context: Context, list: ArrayList<Data>) :
RecyclerView.Adapter<RecyclerView.ViewHolder>() {
companion object {
const val THE_FIRST_VIEW = 1
const val THE_SECOND_VIEW = 2
}
private val yourContext: Context = context
var list: ArrayList<Data> = list
private inner class GfgViewOne(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var gfgText: TextView = itemView.findViewById(R.id.gfgTextView)
fun bind(position: Int) {
val recyclerViewModel = list[position]
gfgText.text = recyclerViewModel.textData
}
}
private inner class View2ViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var gfgText: TextView = itemView.findViewById(R.id.gfgTextView)
fun bind(position: Int) {
val recyclerViewModel = list[position]
gfgText.text = recyclerViewModel.textData
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
if (viewType == VIEW_TYPE_ONE) {
return GfgViewOne(
LayoutInflater.from(context).inflate(R.layout.gfgParentOne, parent, false)
)
}
return View2ViewHolder(
LayoutInflater.from(context).inflate(R.layout.item_view_2, parent, false)
)
}
override fun getItemCount(): Int {
return list.size
}
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (list[position].viewType === VIEW_TYPE_ONE) {
(holder as GfgViewOne).bind(position)
} else {
(holder as View2ViewHolder).bind(position)
}
}
override fun getItemViewType(position: Int): Int {
return list[position].viewType
}
}
Incorporating RecyclerView into our project
Now, in the activity main.xml file, include RecyclerView. The code for activity main.xml is as follows:
XML
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".gfgMainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/someRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Now all that remains is to add the data to the views. The code for MainActivity is as follows:
Kotlin
class GeeksforGeeksActivity : AppCompatActivity() {
private lateinit var gfgRecycler: GfgRecycler
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.gfgMain)
val mydataList = ArrayList<Data>()
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_ONE, "1. Geeks View 1"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_TWO, "2. Geeks View 2"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_ONE, "3. Geeks View 3"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_TWO, "4. Geeks View 4"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_ONE, "5. Geeks View 5"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_TWO, "6. Geeks View 6"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_ONE, "7. Geeks View 7"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_TWO, "8. Geeks View 8"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_ONE, "9. Geeks View 9"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_TWO, "10. Geeks View 10"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_ONE, "11. Geeks View 11"))
dataList.add(Data(GfgRecyclerAdapter.VIEW_TYPE_TWO, "12. Geeks View 12"))
val adapter = GfgRecyclerAdapter(this, dataList)
gfgRecycler = findViewById(R.id.gfgRecycler)
gfgRecycler.layoutManager = LinearLayoutManager(this)
gfgRecycler.adapter = adapter
}
}
Output:
Now when we run our app we see the below screen:
Image #1: The schematic output of the code
Similar Reads
RecyclerView using ListView in Android With Example
RecyclerView is a more flexible and advanced version of ListView and GridView. RecyclerView is used for providing a limited window to a large data set, which means it is used to display a large amount of data that can be scrolled very efficiently by maintaining a limited number of Views. In Recycler
5 min read
Pull to Refresh with RecyclerView in Android with Example
The SwipeRefreshLayout widget is used for implementing a swipe-to-refresh user interface design pattern. Where the user uses the vertical swipe gesture to refresh the content of the views. The vertical swipe is detected by the SwipeRefreshLayout widget and it displays a distinct progress bar and tri
7 min read
RecyclerView in Android with Example
RecyclerView is a ViewGroup added to the android studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item which can be custo
7 min read
Android | Horizontal RecyclerView with Examples
Recycler View is a ViewGroup added to Android Studio as a successor of the GridView and ListView. It is an improvement on both of them and can be found in the latest v-7 support packages. It has been created to make possible construction of any lists with XML layouts as an item which can be customiz
4 min read
RecyclerView as Staggered Grid in Android With Example
GridView: A ViewGroup that shows the items in a two-dimensional scrolling grid. In Grid View, each grid is of the same size. i.e., the height and width of each grid are equal. It shows symmetric items in the views. Staggered GridView: This ViewGroup is the extension of Grid View. In this view, the G
6 min read
CardView using RecyclerView in Android with Example
RecyclerView is an extended version of ListView and GridView. It works on the ViewHolder design pattern. With the help of RecyclerView, we can add many extra features to our list of data. Before starting our example on the implementation of CardView in RecyclerView. We should know what CardView and
9 min read
Expandable RecyclerView in Android with Kotlin
In this article, we will get to know how to implement the expandable RecyclerView. In General, we will show the list of items in the listview but in some situations like if you want to show the sub-list items, then we have to use an expandable list. See the below image for a better understanding. ex
7 min read
Two Dimensional RecyclerView in Android with Example
In many cases, we have to display the horizontally scrollable list in such a way that we also have to show headers on it to indicate the list belongs to which category. So for that, we have to create a two-dimensional Recycler-View in our app. In this article, we will take a look at creating a 2-dim
9 min read
SearchView in Android with RecyclerView
Many apps represent data in the form of huge lists and for filtering these lists we have seen the SearchView present inside these apps. So for filtering this list of data we generally use a SearchView. In this article, we will take a look at the implementation of Search View in Android with a Recycl
10 min read
Android RecyclerView Load More on Scroll with Example
Many apps display so many amounts of data in the form of a list. This data is so much so that it cannot be loaded at a time. If we load this data at a time then it may take so much loading time and degrades the performance of our RecyclerView. So to solve this we generally load the data in chunks an
8 min read