Android RecyclerView Kotlin Example with model class

06-Dec-2024

Android RecyclerView Kotlin Example with model class


1/Step:  Create A New Project and add the code activity_main.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/rlMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>



2/Step :  Add the code MainActivity.kt 



package com.alomkaisa.myapplication

import MoviesAdapter
import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView

class MainActivity : AppCompatActivity() {
private val movieList = ArrayList<MovieModel>()
private lateinit var moviesAdapter: MoviesAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val recyclerView: RecyclerView = findViewById(R.id.recyclerView)
moviesAdapter = MoviesAdapter(movieList)
val layoutManager = LinearLayoutManager(applicationContext)
recyclerView.layoutManager = layoutManager
recyclerView.itemAnimator = DefaultItemAnimator()
recyclerView.adapter = moviesAdapter
prepareMovieData()
}
private fun prepareMovieData() {
var movie = MovieModel("Mad Max: Fury Road", "Action & Adventure", "2024")
movieList.add(movie)
movie = MovieModel("Inside Out", "Animation, Kids & Family", "2030")
movieList.add(movie)
movie = MovieModel("Star Wars: Episode VII - The Force Awakens", "Action", "2023")
movieList.add(movie)
movie = MovieModel("Shaun the Sheep", "Animation", "2025")
movieList.add(movie)
movie = MovieModel("The Martian", "Science Fiction & Fantasy", "2011")
movieList.add(movie)
movie = MovieModel("Mission: Impossible Rogue Nation", "Action", "2015")
movieList.add(movie)
movie = MovieModel("Up", "Animation", "2019")
movieList.add(movie)
movie = MovieModel("Star Trek", "Science Fiction", "2019")
movieList.add(movie)
movie = MovieModel("The LEGO MovieModel", "Animation", "2011")
movieList.add(movie)
movie = MovieModel("Iron Man", "Action & Adventure", "2008")
movieList.add(movie)
movie = MovieModel("Aliens", "Science Fiction", "1986")
movieList.add(movie)
movie = MovieModel("Chicken Run", "Animation", "2000")
movieList.add(movie)
movie = MovieModel("Back to the Future", "Science Fiction", "1985")
movieList.add(movie)
movie = MovieModel("Raiders of the Lost Ark", "Action & Adventure", "1981")
movieList.add(movie)
movie = MovieModel("Goldfinger", "Action & Adventure", "1965")
movieList.add(movie)
movie = MovieModel("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014")
movieList.add(movie)
movie = MovieModel("Goldfinger", "Action & Adventure", "1965")
movieList.add(movie)
movie = MovieModel("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014")
movieList.add(movie)
movie = MovieModel("Goldfinger", "Action & Adventure", "1965")
movieList.add(movie)
movie = MovieModel("Guardians of the Galaxy", "Science Fiction & Fantasy", "2014")
movieList.add(movie)
moviesAdapter.notifyDataSetChanged()
}
}


3/Step :  Create A New Class MovieModel.kt and add the code.



package com.alomkaisa.myapplication

class MovieModel(title: String?, genre: String?, year: String?) {
private var title: String
private var genre: String
private var year: String
init {
this.title = title!!
this.genre = genre!!
this.year = year!!
}
fun getTitle(): String? {
return title
}
fun setTitle(name: String?) {
title = name!!
}
fun getYear(): String? {
return year
}
fun setYear(year: String?) {
this.year = year!!
}
fun getGenre(): String? {
return genre
}
fun setGenre(genre: String?) {
this.genre = genre!!
}
}



4/Step :   Create A New Class MoviesAdapter and add the code .



import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.annotation.NonNull
import androidx.recyclerview.widget.RecyclerView
import com.alomkaisa.myapplication.MovieModel
import com.alomkaisa.myapplication.R

internal class MoviesAdapter(private var moviesList: List<MovieModel>) :
RecyclerView.Adapter<MoviesAdapter.MyViewHolder>() {
internal inner class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
var title: TextView = view.findViewById(R.id.title)
var year: TextView = view.findViewById(R.id.year)
var genre: TextView = view.findViewById(R.id.genre)
}
@NonNull
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val itemView = LayoutInflater.from(parent.context)
.inflate(R.layout.item_row, parent, false)
return MyViewHolder(itemView)
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
val movie = moviesList[position]
holder.title.text = movie.getTitle()
holder.genre.text = movie.getGenre()
holder.year.text = movie.getYear()
}
override fun getItemCount(): Int {
return moviesList.size
}
}


5/Step :   Create A New Layout resource file item_row.xml 


<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="100dp"
android:layout_margin="8dp"
app:cardBackgroundColor="#120E0E">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_toStartOf="@+id/year"
android:textColor="@android:color/white"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="@+id/year"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:textColor="@android:color/white" />
<TextView
android:id="@+id/genre"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textColor="@android:color/white" />
</RelativeLayout>
</androidx.cardview.widget.CardView>


Output : 


Comments