Basic RecyclerView Custom Adapter in Kotlin Android

We use RecyclerView in Android to show a list or grid of items. For that, we have to write the RecyclerView Custom Adapter to populate data to each item of the list. In the following code, I have shared a very simple & basic RecyclerView custom adapter in Kotlin. You can just copy-paste this code in your project.

This article contains only CustomAdapter code, here you can also find a Detailed Example to Show Vertical List in Kotlin using RecyclerView.

A Complete RecyclerView Video Tutorial Series For Kotlin

Following is the Kotlin code, If you are looking for a Java code, please find it at this link Basic RecyclerView Custom Adapter in JAVA.

Basic RecyclerView Custom Adapter in Kotlin

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import java.util.ArrayList
import androidx.recyclerview.widget.RecyclerView

class CustomAdapter(private val context: Context, private val list: ArrayList<String>) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

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

        internal var yourView: TextView

        init {
            yourView = itemView.findViewById(R.id.your_textview_id) // 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 ViewHolder(LayoutInflater.from(context).inflate(R.layout.your_layout_xml_file, parent, false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        (holder as ViewHolder).bind(position)
    }

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

It’s a sample code in which you need to change your “your_layout_xml_file” with your layout XML file reference. In this code, only 1 TextView is considered in the list item. But you can customize it according to your layout. Also in the constructor String ArrayList is passed, but you can pass your Data Class ArrayList.

To initialize RecyclerView and assign this CustomAdapter to you can use the following Kotlin code.

var recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
val adapter = CustomAdapter(this, YOUR_ARRAY_LIST)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = adapter

That’s it. Here you can find many other helping tutorials.
Handy Opinion Tutorials

Next:
Convert Bitmap to File in Android – JAVA & Kotlin

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.

3 thoughts on “Basic RecyclerView Custom Adapter in Kotlin Android

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: