Android RecyclerView in Kotlin :

 

Recyclerview kotlin example :


Step-1;Create Activity Main.Xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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"
android:orientation="vertical"
tools:context=".MainActivity">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:itemCount="5"
tools:listitem="@layout/card_view_design" />

</LinearLayout>

Step-2:Create Model Class :

data class ItemsViewModel(val image: Int, val text: String){

}

Step-3:Create Adapter  :

package com.like.examplekotlin
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class CustomAdapter(private val mList: List<ItemsViewModel>)
: RecyclerView.Adapter<CustomAdapter.ViewHolder>() {

// create new views
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
// inflates the card_view_design view that is used to hold list item
val view = LayoutInflater.from(parent.context)
.inflate(R.layout.card_view_design, parent, false)

return ViewHolder(view)
}

// binds the list items to a view
override fun onBindViewHolder(holder: ViewHolder, position: Int) {

val ItemsViewModel = mList[position]

// sets the image to the imageview from our itemHolder class
holder.imageView.setImageResource(ItemsViewModel.image)

// sets the text to the textview from our itemHolder class
holder.textView.text = ItemsViewModel.text

}

// return the number of the items in the list
override fun getItemCount(): Int {
return mList.size
}

// Holds the views for adding it to image and text
class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) {
val imageView: ImageView = itemView.findViewById(R.id.imageview)
val textView: TextView = itemView.findViewById(R.id.textView)
}
}

Step-4:Create MainActivity.kt :

package com.like.examplekotlin

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
import android.widget.Toast.LENGTH_LONG
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {


override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.
activity_main)
// getting the recyclerview by its id
val recyclerview = findViewById<RecyclerView>(R.id.recyclerview)

// this creates a vertical layout Manager
recyclerview.layoutManager = LinearLayoutManager(this)

// ArrayList of class ItemsViewModel
val data = ArrayList<ItemsViewModel>()


data.add(ItemsViewModel(R.drawable.raj,"First Items"))
data.add(ItemsViewModel(R.drawable.
raj,"second Items"))
data.add(ItemsViewModel(R.drawable.
raj,"third Items"))
data.add(ItemsViewModel(R.drawable.
raj,"fourth Items"))
data.add(ItemsViewModel(R.drawable.
raj,"Fifth Items"))
data.add(ItemsViewModel(R.drawable.
raj,"six Items"))
//if we don't want put manually data on recyclerview we use for loop also:
        // This loop will create 20 Views containing
// the image with the count of view
/* for (i in 1..20) {
data.add(ItemsViewModel(R.drawable.raj, "Item " + i))
}*/


// This will pass the ArrayList to our Adapter
val adapter = CustomAdapter(data)

// Setting the Adapter with the recyclerview
recyclerview.adapter = adapter

}
}
 step-4 :dependencies :
dependencies{
// for adding recyclerview
implementation 'androidx.recyclerview:recyclerview:1.2.0'

// for adding cardview
implementation 'androidx.cardview:cardview:1.0.0'
}

Rajeshbhatt12

My name is Rajesh Bhatt. I am working as a senior android developer . I have created this blog for kotlin ,java and Android Development interview questions etc..

Previous Post Next Post