Custom Adapter with Multiple View Types for RecyclerView in Kotlin – Android

As we all know that RecyclerView is a UI component to show lists in Android. In this tutorial, you will learn, How to create a Custom RecyclerView Adapter with Multiple View Types in Kotlin Android. Following is the sample RecyclerView Custom Adapter. It includes multiple view types and written in Kotlin. But if you are looking for a step by step guide with an example, take a look at another article, Multiple View Types in RecyclerView with Chat App Example.

Following is the Kotlin code. Here you can also find Java Adapter to Add Multiple View Types in RecyclerView.

Custom Adapter with Multiple List Items in Kotlin
RecyclerView with Multiple View Types

Sample Custom Adapter with Multiple View Types In Android – Kotlin

class CustomAdapter(
    private val context: Context,
    internal var list: ArrayList<String> // ArrayList of your Data Model
)// you can pass other parameters in constructor
    : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

    internal val VIEW_TYPE_ONE = 1
    internal val VIEW_TYPE_TWO = 2

    private inner class ViewHolder1 internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {

        internal var yourView: TextView

        init {
            yourView = itemView.findViewById(R.id.yourView) // Initialize your All views prensent in list items
        }

        internal fun bind(position: Int) {
            // This method will be called anytime a list item is created or update its data
            //Do your stuff here
            yourView.text = list[position]
        }
    }

    private inner class ViewHolder2 internal constructor(itemView: View) : RecyclerView.ViewHolder(itemView) {

        internal var yourView: TextView

        init {
            yourView = itemView.findViewById(R.id.yourView) // Initialize your All views prensent in list items
        }

        internal fun bind(position: Int) {
            // This method will be called anytime a list item is created or update its data
            //Do your stuff here
            yourView.text = list[position]
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return if (viewType == VIEW_TYPE_ONE) {
            ViewHolder1(LayoutInflater.from(context).inflate(R.layout.your_list_item_1, parent, false))
        } else ViewHolder2(LayoutInflater.from(context).inflate(R.layout.your_list_item_2,parent,false)) //if it's not VIEW_TYPE_ONE then its VIEW_TYPE_TWO
   }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if (list[position].type == Something) { // put your condition, according to your requirements
            (holder as ViewHolder1).bind(position)
        } else {
            (holder as ViewHolder2).bind(position)
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }

    override fun getItemViewType(position: Int): Int {
        // here you can get decide from your model's ArrayList, which type of view you need to load. Like
        return if (list[position].type === Something) { // put your condition, according to your requirements
            VIEW_TYPE_ONE
        } else VIEW_TYPE_TWO
    }
}

Don’t forget to add the following RecyclerView dependency in your App level build.gradle file

implementation "androidx.recyclerview:recyclerview:1.0.0" 
// or if you are using support library instead of Androidx then 
// implementation 'com.android.support:recyclerview-v7:28.0.0'

In the above sample Custom Adapter class, according to your requirements, you need to change layout names & conditions. It’s also mentioned in the comments. You can use this sample class in your all projects wherever you need to implement RecyclerView.

If it’s not clear, have a look at our step by step tutorial, Multiple View Types in RecyclerView with Chat App Example.

Enjoy 🙂

Don’t forget to visit our Coding Articles & Tutorials Knowledge Base for other helping code snippets.

Please share this post:
Native Mobile Application Developer (Android + IOS) having experience in Java, Swift, Kotlin, Objective C, Unity, C#, C/C++, NODE JS & PHP.

4 thoughts on “Custom Adapter with Multiple View Types for RecyclerView in Kotlin – Android

  1. Usually I do not read post on blogs, however I wish to say that this write-up very pressured me to take a look at and do it! Your writing style has been surprised me. Thanks, very great post.

Ask a Question

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

%d bloggers like this: