Recycler View Android Kotlin

Recycler View Tutorial






Create a Android Studio Project :

MainActivity.kt

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

class MainActivity : AppCompatActivity() {
private lateinit var editItem:EditText
private lateinit var btnAdd:Button
private lateinit var recyclerView: RecyclerView

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


}

private fun fetchData():ArrayList<String>{
val list = ArrayList<String>()
for(i in 0 until 100){
list.add("Item No : $i")
}
return list
}
}


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">

<EditText
android:id="@+id/edit_item"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="36dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="16dp"
android:ems="10"
android:hint="Enter Todo..."
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="@+id/text_view"
app:layout_constraintEnd_toStartOf="@+id/btn_add"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />

<Button
android:id="@+id/btn_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:text="Add"
app:layout_constraintBottom_toBottomOf="@+id/edit_item"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="@+id/edit_item"
app:layout_constraintTop_toTopOf="@+id/edit_item"
app:layout_constraintVertical_bias="0.0" />

<TextView
android:id="@+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:layout_marginBottom="36dp"
android:fontFamily="sans-serif-medium"
android:text="Your List"
android:textColor="@android:color/background_dark"
android:textSize="18sp"
app:layout_constraintBottom_toTopOf="@+id/recycler_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/edit_item" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginTop="28dp"
android:layout_marginBottom="16dp"
android:paddingHorizontal="24dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_view" />
</androidx.constraintlayout.widget.ConstraintLayout

2. Now Our Basic App is Ready. Create a new Kotlin class ListItemAdapter which extends RecyclerView Adapter.

ListItemAdapter.kt

import android.view.LayoutInflater
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.content.res.ColorStateListInflaterCompat.inflate

class ItemListAdapter(val items:ArrayList<String>) : RecyclerView.Adapter<ItemViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_recycler,parent,false);
return ItemViewHolder(view)
}

override fun getItemCount(): Int {
return items.size
}

override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
val currentItem = items[position]
holder.titleView.text = currentItem
}

}

class ItemViewHolder(itemView:View): RecyclerView.ViewHolder(itemView) {
val titleView: TextView = itemView.findViewById(R.id.txt_item_name);

}

3.Now Create a new layout which the recycler view will display.
Create item_list.xml

item_list.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<TextView
android:id="@+id/txt_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:paddingHorizontal="20dp"
android:paddingVertical="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>












Comments

Popular posts from this blog

Temperature Converter App : Android Kotlin App

Making your First App: Dice Roller App

Android Data Binding