Android Kotlin: RecyclerView divider line

Adding Divider Lines Between RecyclerView Items in Android (Kotlin)

This code demonstrates how to display divider lines between items in a RecyclerView using Kotlin in an Android application. It achieves this by utilizing the DividerItemDecoration class provided by the Android framework.

Components Breakdown:

The code consists of three parts:

  1. MainActivity.kt: This file manages the overall activity lifecycle and configures the RecyclerView.
  2. RecyclerViewAdapter.kt: This file defines the adapter responsible for populating the RecyclerView with data and displaying individual items.
  3. activity_main.xml and custom_view.xml: These XML files define the layout structure for the activity and each item displayed in the RecyclerView.

Summary

The MainActivity.kt first initializes a list of animal names and sets up the RecyclerView with a LinearLayoutManager for vertical orientation. It then creates an adapter instance with the animal data and sets it on the RecyclerView. Finally, it creates a DividerItemDecoration object, passing the context and layout manager orientation. This decoration is then added to the RecyclerView, resulting in divider lines between each item.

The RecyclerViewAdapter.kt inflates a custom view for each item displaying the animal name from the data list. It also implements methods to manage the number of items and their unique IDs.

This example provides a solid foundation for customizing the appearance and behavior of dividers in your RecyclerView by further modifying the DividerItemDecoration properties or creating a custom decoration class.


MainActivity.kt

package com.cfsuman.kotlintutorials

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView


class MainActivity : AppCompatActivity() {
    private lateinit var context:MainActivity

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Get the context
        context = this;

        // Get the widgets reference from XML layout
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)


        // initialize a mutable list of animals
        val animals = mutableListOf(
            "Aardvark", "Albatross", "Alligator",
            "Alpaca", "Ant", "Anteater",
            "Barracuda", "Bear", "Dog",
            "Cat", "Cow"
        )

        // initialize an instance of linear layout manager
        val layoutManager = LinearLayoutManager(
            this,
            RecyclerView.VERTICAL,
            false
        ).apply {
            // specify the layout manager for recycler view
            recyclerView.layoutManager = this
        }

        // finally, data bind the recycler view with adapter
        RecyclerViewAdapter(animals).apply {
            recyclerView.adapter = this
        }


        // initialize an instance of divider item decoration
        DividerItemDecoration(
            this, // context
            layoutManager.orientation
        ).apply {
            // add divider item decoration to recycler view
            // this will show divider line between items
            recyclerView.addItemDecoration(this)
        }
    }
}
RecyclerViewAdapter.kt

package com.cfsuman.kotlintutorials

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView


class RecyclerViewAdapter(private val animals: MutableList<String>)
    : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {

    override fun onCreateViewHolder(
        parent: ViewGroup, viewType: Int): ViewHolder {
        // inflate the custom view from xml layout file
        val view: View = LayoutInflater.from(parent.context)
            .inflate(R.layout.custom_view,parent,false)

        // return the view holder
        return ViewHolder(view)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        // display the current animal
        holder.animal.text = animals[position]
    }


    override fun getItemCount(): Int {
        // number of items in the data set held by the adapter
        return animals.size
    }


    class ViewHolder(itemView: View)
        : RecyclerView.ViewHolder(itemView){
        val animal: TextView = itemView.findViewById(R.id.tvAnimal)
    }


    // this two methods useful for avoiding duplicate item
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }


    override fun getItemViewType(position: Int): Int {
        return position
    }
}
activity_main.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="match_parent"
    android:id="@+id/rootLayout"
    android:background="#DCDCDC">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
custom_view.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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/tvAnimal"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:fontFamily="sans-serif"
        android:textSize="24sp"
        android:padding="24dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
More android kotlin tutorials