In this tutorial, we will learn how to use Recycler View in Android using Kotlin. A RecyclerView is an advanced version of a ListView that performs better.
Android RecyclerView allows you to create a horizontal, vertical, or Expandable List. It’s most useful when dealing with data sets whose elements can change in runtime as a result of user actions or network events.
Note: When you need to show a large number of things and the number is changing, you should use RecyclerView. ListView should only be used when the number of items is constant and the screen size is limited.
Preview: Android RecyclerView
<–RecyclerView Example
RecyclerView code:
<androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="match_parent" android:layout_height="match_parent" />
dependency for recyclerView
–> Add the given below code to your file build.gradle.(app)
//recycler view implementation "androidx.recyclerview:recyclerview:1.2.1"
File: activity_main.xml
–> Add the given below code to your file 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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <androidx.recyclerview.widget.RecyclerView android:id="@+id/recyclerView" android:layout_width="0dp" android:layout_height="0dp" tools:listitem="@layout/item_list" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
File: Item_list.xml
–> Create a new Layout Resource File and Add the given below code to your file.
<?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="wrap_content" android:layout_marginTop="10dp" android:layout_marginBottom="10dp" android:background="@color/teal_200" android:padding="16dp"> <ImageView android:id="@+id/image" android:layout_width="0dp" android:layout_height="200dp" android:scaleType="centerCrop" android:src="@drawable/ic_launcher_foreground" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/firstname" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_marginTop="12dp" android:ellipsize="end" android:gravity="center_horizontal" android:maxLines="2" android:text="Developers Dome" android:textAlignment="center" android:textColor="#212121" android:textSize="18sp" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toBottomOf="@id/image" /> </androidx.constraintlayout.widget.ConstraintLayout>
File: MainActivity.kt
–> Add the given below code to your file MainActivity.kt
package com.sagar.recyclerview import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.view.Window import android.view.WindowManager import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView class MainActivity : AppCompatActivity() { private lateinit var mAdapter: RecyclerViewAdapter override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE); this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) setContentView(R.layout.activity_main) //Layout manager val recyclerView = findViewById<RecyclerView>(R.id.recyclerView) recyclerView.layoutManager = LinearLayoutManager(this) val item = ArrayList<User>() //adding user item.add(User("Developers")) item.add(User("Dome")) item.add(User("Developers1")) item.add(User("Dome1")) item.add(User("Developers2")) item.add(User("Dome2")) item.add(User("Developers3")) item.add(User("Dome3")) //Adapter setting mAdapter = RecyclerViewAdapter(item) recyclerView.adapter = mAdapter } }
File: User.kt
–> Create a New Kotlin Data Class and Add the given below code to your file .
package com.sagar.recyclerview data class User(val Firstname: String)
File: RecyclerViewAdapter.kt
–> Create a New Kotlin Class and Add the given below code to your file.
package com.sagar.recyclerview import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView class RecyclerViewAdapter(val userList: ArrayList<User>) : RecyclerView.Adapter<RecyclerViewAdapter.ViewHolder>() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewAdapter.ViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.item_list, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: RecyclerViewAdapter.ViewHolder, position: Int) { holder.bind(userList[position]) } override fun getItemCount(): Int { return userList.size } class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { fun bind(user: User) { val textViewName = itemView.findViewById<TextView>(R.id.firstname) textViewName.text = user.Firstname } } }
Data Binding in Android with Example
Webview Example Tutorial in Android Studio
Pingback: Android cardView | Android Recyclerview using cardView
Pingback: Material Dialogs in Android with Example - Developers Dome
Pingback: Shared preference in Android tutorial - Developers Dome
Pingback: Difference between Linear layout and Relative layout
Pingback: RecyclerView GridView Android Example in Kotlin - Developers Dome
Pingback: GET request using retrofit in Android Tutorial - Developers Dome
Pingback: Using DiffUtil in Android RecyclerView Example - Developers Dome