RecyclerView With Kotlin – Android

We know that if we need to display a scrolling list of elements based on a large set of data sets which may frequently change we should use RecyclerView.

RecyclerView is a much-advanced version of ListView with a lot of improvements made. You might have Implemented RecyclerView in android with Java but here we are going to learn how to implement a RecyclerView with Kotlin.

Lets go for a detailed tutorial for implementation of RecyclerView with Kotlin Android.

Import the following dependency to the app level build.gradle to add the RecyclerView In your project.


implementation ‘com.android.support:recyclerview-v7:27.1.1’

N we are going to set up the MainActivity layout file activity_main.xml. The Layout consists of a RecyclerView as given below.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".activity.MainActivity">

   <android.support.v7.widget.RecyclerView
       android:id="@+id/recycler_view"
       android:layout_width="match_parent"
       android:layout_height="wrap_content">

   </android.support.v7.widget.RecyclerView>

</android.support.constraint.ConstraintLayout>

Now we are going to set up a list_item_view for RecyclerView which includes only one TextView. The layout file is given below.

list_item_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/title"
        android:textSize="18sp"
        android:padding="6dp"
        android:maxLines="1"
        android:layout_gravity="center"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
    <View
        android:background="#000"
        android:layout_width="match_parent"
        android:layout_height="1dp"/>

</LinearLayout>

To populate our data with RecyclerView, We need a RecyclerViewAdapter class RecyclerViewAdapter.kt. Which is given below. This class inherited from RecyclerView.Adapter() . It has three abstract methods.

  • getItemCount(): Returns the total number of items in the data set held by the adapter. In our project, we return the size of our dataList Array.
  • onCreateViewHolder(parent : ViewGroup, viewType:int) ; Viewholder :
    This method onCreateViewHolder(ViewGroup, int) create a RecyclerView.ViewHolder initializes some private fields to be used by RecyclerView.
  • onBindViewHolder(holder:,position :int) :
    This method internally onBindViewHolder(ViewHolder, int) update RecyclerView.ViewHolder with the item at the given position and also sets up some private fields to be used by RecyclerView.  in this method we can set up our TextView or any other  Views with our data.

Our RecyclerViewAdapter.kt is shown below.

RecyclerViewAdapter.kt

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.TextureView
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.shrishtionline.recyclerviewwithkotlin.R

class RecyclerViewAdapter(private var context: Context,private var dataList:ArrayList<String>):RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() {
    override fun getItemCount(): Int {
        return dataList.size;
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        return ViewHolder(LayoutInflater.from(context).inflate(R.layout.list_item_view, parent, false))
    }

    override fun onBindViewHolder(holder:ViewHolder, position: Int) {
        holder.textView.text=dataList.get(position);
    }


    class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        var textView:TextView =itemView!!.findViewById(R.id.text_view)
    }
}

Now we need to set up our MainActivity.kt class.

  • The class includes a function addAnimals() to add some data to our list.

MainActivity.kt

package com.shrishtionline.recyclerviewwithkotlin

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView

class MainActivity : AppCompatActivity() {

    lateinit var recyclerView: RecyclerView
    lateinit var adapter: RecyclerViewAdapter
    var dataList:ArrayList<String> = ArrayList();

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

        //Initializing RecyclerView
        recyclerView=findViewById(R.id.recycler_view)


        adapter= RecyclerViewAdapter(this,dataList)

        //Set up recyclerview with Vertical LayoutManager and the adapter

        recyclerView.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)

        //Adding animal names
        addAnimals();

        // Notify the adapter for data change.
        adapter.notifyDataSetChanged()

    }

    private fun addAnimals() {
        dataList!!.add("Dog")
        dataList!!.add("Cat")
        dataList!!.add("Monkey")
        dataList!!.add("lion")
        dataList!!.add("Elephent")
        dataList!!.add("Cheetah")
        dataList!!.add("Snake")
        dataList!!.add("Cow")
        dataList!!.add("Ant")
        dataList!!.add("Tiger")
        dataList!!.add("Lizard")

    }
}

That’s it! Now run the project!

The full source code is available here.

Thanks for reading!