Kotlin RecyclerView Examples – 3 Practical Use Cases

Discover three practical examples of Kotlin RecyclerView, designed for beginners to enhance their app development skills.
By Taylor

Understanding Kotlin RecyclerView

RecyclerView is a powerful component in Android that allows you to display large sets of data efficiently. It provides a flexible way to create lists and grids, and it’s especially useful when you need to display dynamic content. In this guide, we will explore three practical examples of Kotlin RecyclerView to help you get started.

Example 1: Simple List of Strings

Context

This example demonstrates how to create a basic RecyclerView that displays a simple list of strings. It’s perfect for beginners wanting to understand the fundamentals of RecyclerView.

// In your build.gradle file, add the RecyclerView dependency
implementation 'androidx.recyclerview:recyclerview:1.2.1'

// Create a layout file for the RecyclerView item (item_view.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp">
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp" />
</LinearLayout>

// Define your data source
val items = listOf("Item 1", "Item 2", "Item 3", "Item 4")

// Create a RecyclerView Adapter
class SimpleAdapter(private val itemList: List<String>) : RecyclerView.Adapter<SimpleAdapter.ViewHolder>() {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView = view.findViewById(R.id.text_view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_view, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textView.text = itemList[position]
    }

    override fun getItemCount() = itemList.size
}

// Set up the RecyclerView in your Activity or Fragment
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = SimpleAdapter(items)

Notes

You can easily modify the data source to include more items or change the layout to customize the appearance. This is a foundational example that you can build upon.

Example 2: RecyclerView with Click Listener

Context

In this example, we enhance our previous list by adding a click listener to each item. This is useful for interactive applications where you want to respond to user actions.

// Item layout remains the same as item_view.xml

// Modify the Adapter to include click handling
class ClickableAdapter(private val itemList: List<String>, private val clickListener: (String) -> Unit) : RecyclerView.Adapter<ClickableAdapter.ViewHolder>() {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val textView: TextView = view.findViewById(R.id.text_view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_view, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.textView.text = itemList[position]
        holder.itemView.setOnClickListener { clickListener(itemList[position]) }
    }

    override fun getItemCount() = itemList.size
}

// Set up the RecyclerView with click listener in your Activity or Fragment
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = ClickableAdapter(items) { selectedItem ->
    Toast.makeText(this, "Clicked: $selectedItem", Toast.LENGTH_SHORT).show()
}

Notes

This example demonstrates how to make your RecyclerView interactive. You can replace the Toast with navigation to another screen or any other action based on your app’s requirements.

Example 3: RecyclerView with Image and Text

Context

In this more advanced example, we will create a RecyclerView that displays a list of items, each containing an image and a title. This is useful for apps that display media content or products.

// Create a layout file for the item with image and text (item_image_view.xml)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">
    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:scaleType="centerCrop" />
    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="18sp" />
</LinearLayout>

// Define a data class for items with image resource and title
data class ImageItem(val imageResId: Int, val title: String)
val imageItems = listOf(
    ImageItem(R.drawable.image1, "Title 1"),
    ImageItem(R.drawable.image2, "Title 2"),
    ImageItem(R.drawable.image3, "Title 3")
)

// Create the Adapter for the new layout
class ImageAdapter(private val itemList: List<ImageItem>) : RecyclerView.Adapter<ImageAdapter.ViewHolder>() {
    class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        val imageView: ImageView = view.findViewById(R.id.image_view)
        val textView: TextView = view.findViewById(R.id.text_view)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_image_view, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.imageView.setImageResource(itemList[position].imageResId)
        holder.textView.text = itemList[position].title
    }

    override fun getItemCount() = itemList.size
}

// Set up the RecyclerView with image items in your Activity or Fragment
val recyclerView: RecyclerView = findViewById(R.id.recycler_view)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.adapter = ImageAdapter(imageItems)

Notes

This example showcases how to work with images in RecyclerView. Make sure to have the images in your drawable folder. You can further enhance this by adding click listeners or animations as needed.


By understanding these examples of Kotlin RecyclerView, you will be well-equipped to implement lists in your Android applications. Whether you’re displaying simple text, interactive items, or media content, RecyclerView is a versatile tool for any developer.