RecyclerView Application example using kotlin and viewbinding.

Overview

SimpleRecyclerViewApp

Shows how to display some items in a simple list using the RecyclerView and RecycleView.Adapter.

The RecycleView is the entity that displays the list to the user on the layout of your Ui.
The RecyclerView.Adapter<T> is the entity that adapts and manipulates to the recycler to be displayed on the user interface later when rendering the android view, it also updates the data with their respective views.

Prepare your Ui :

  • Create a MainActivity class :
package com.example.session2

import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    }
}
  • Define the activity inside the AndroidManifest.xml :
<activity
  android:name=".MainActivity"
  android:exported="true">
</activity>
  • Define an Action main intent filter with a launcher category (build a launcher activity when the user clicks the app icon in the app drawer) :
<activity
  android:name=".MainActivity"
  android:exported="true">
    <intent-filter>
      <action android:name="android.intent.action.MAIN" />

      <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>
  • Define the binding framework from the jetpack compose (shortcut to accessing views with their IDs directly and their root easily) : In app/gradle.build android block :
android {
    ...
    buildFeatures {
        viewBinding true
    }
}
  • Naviagte to app/src/main/res/layout/activity_main.xml and add the context to the root layout :
tools:context=".MainActivity"
  • Add a recyclerview to your xml file with an id.
  • The final activity_main.xml file should look like that :
<?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/recycler"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/item_model" />
</LinearLayout>
  • Navigate to the onCreate(onSavedInstance: Bundle) method, and set your xml file using setContentView() and the inflated layout :
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
  • Now a RecyclerView instance is predefined and the ready to be accessed using its binding reference which returns a recycler view instance.
  • Now setup your recycler LayoutManager and Adapter.
  • LayoutManager : controls how the items are getting viewed to the user (Linear or Grid).
  • RecyclerView.Adapter : holds the data to be adapted later on the user interface.
  • The RecyclerView.Adapter accepts an array of any type as a parameter -- the array is the data that would be adapted onto the ui later when instantiating the android view.
  • The LayoutManager can be a LinearLayoutManager for displaying items underneath each other, or GridLayoutManager for displaying items in a grid fashion.
  • The final setup so far :
class MainActivity : AppCompatActivity() {
    
    private lateinit var binding: ActivityMainBinding
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)
        supportActionBar?.title = "First App"
        
        val array = ArrayList<ItemModel>()
        array.add(ItemModel(R.drawable.ic_launcher_background, "Ahmed"))
        array.add(ItemModel(R.drawable.ic_launcher_background, "Abdo"))
        array.add(ItemModel(R.drawable.ic_launcher_background, "Eman"))
        
        val adapter = Adapter(array)
        binding.recycler.layoutManager = LinearLayoutManager(this)
        binding.recycler.adapter = adapter
    }
}

Bind data to the Ui :

  • Create a DataModel class and this class would hold the data for each position to be rendered on its respective android views :
data class ItemModel(val photo: Int, val name: String)
  • Create a RecyclerView.ViewHolder that holds the definition of each children views on each position with the recycler list :
import androidx.recyclerview.widget.RecyclerView

class ViewHolder(binding: ItemModelBinding) : RecyclerView.ViewHolder(binding.root) {
        val photo = binding.image
        val name = binding.textView
}
  • Create a RecyclerView.Adapter that would return an instance of a ViewHolder and adapters data onto the children views on each position :
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
import com.example.session2.databinding.ItemModelBinding

class Adapter(private val list: ArrayList<ItemModel>) : RecyclerView.Adapter<Adapter.ViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val inflatedView = ItemModelBinding.inflate(LayoutInflater.from(parent.context))
        return ViewHolder(inflatedView, parent, false))
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = list[position]
        holder.name.text = item.name
        holder.photo.setOnClickListener {
            Toast.makeText(it.context, position.toString(), Toast.LENGTH_SHORT).show()
        }
    }

    override fun getItemCount(): Int {
        return list.size
    }
}
You might also like...
Elegant design and convenient to use RecyclerView adapter library based on Kotlin DSL.
Elegant design and convenient to use RecyclerView adapter library based on Kotlin DSL.

xAdapter: Kotlin DSL 风格的 Adapter 封装 1、简介 该项目是 KotlinDSL 风格的 Adapter 框架封装,用来简化 Adapter 调用,思想是采用工厂和构建者方式获取 Adapter 避免代码中定义大量的 Adapter 类。该项目在 BRVAH 的 Ada

A RecyclerView(advanced and flexible version of ListView in Android) with refreshing,loading more,animation and many other features.
A RecyclerView(advanced and flexible version of ListView in Android) with refreshing,loading more,animation and many other features.

UltimateRecyclerView Master branch: Dev branch: Project website:https://github.com/cymcsg/UltimateRecyclerView Description UltimateRecyclerView is a R

kotlin dsl for kids to simplify RecyclerView.Adapter logic

KidAdapter RecyclerView adapter for kids. A kotlin dsl mechanism to simplify and reduce boilerplate logic of a RecyclerView.Adapter. With KidAdapter y

RecyclerView Template With Kotlin
RecyclerView Template With Kotlin

RecyclerViewTemplate-Kotlin How To Create Recycler Adapter Template In Android Studio . RecyclerView makes it easy to efficiently display large sets

Starter code for Android Kotlin Fundamentals Codelab 7.1 RecyclerView Fundamentals

TrackMySleepQuality with RecyclerView - Starter Code for 7.1 Starter code for Android Kotlin Fundamentals Codelab 7.1 RecyclerView Fundamentals Introd

ExpandableRecyclerView - Expandable RecyclerView For Kotlin
ExpandableRecyclerView - Expandable RecyclerView For Kotlin

Expandable RecyclerView ExpandableItemView in ScrollView: ExpandableRecyclerView

how to handle recyclerView in Kotlin to display grid , vertical , horizontal layouts

Dogglers - Starter Code Starter code for the second independent project for Android Basics in Kotlin. Introduction This is the starter code for the Do

RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)
RecyclerView extension library which provides advanced features. (ex. Google's Inbox app like swiping, Play Music app like drag and drop sorting)

Advanced RecyclerView This RecyclerView extension library provides Google's Inbox app like swiping, Play Music app like drag-and-drop sorting and expa

[] Super fast and easy way to create header for Android RecyclerView

DEPRECATED I created this library back in the day when I thought RecyclerView was all new and difficult. Writing an adapter that could inflate multipl

Owner
Google Developers
Versatile tutorials for university students powered by Egyptian developers
Google Developers
This project is a simple recyclerview example coded in kotlin language.

EXPENSIVE CARS ?? This project is a simple recyclerview example coded in kotlin language. My purpose in writing this project is to practice the recycl

Muhammed Mustafa Geldi 2 Nov 1, 2022
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

XRecyclerView 5.3k Dec 26, 2022
A RecyclerView that implements pullrefresh and loadingmore featrues.you can use it like a standard RecyclerView

XRecyclerView a RecyclerView that implements pullrefresh , loadingmore and header featrues.you can use it like a standard RecyclerView. you don't need

XRecyclerView 5.3k Dec 26, 2022
Pagination-RecyclerView - Simple and easy way to Paginating a RecyclerView

Pagination-RecyclerView Simple and easy way to Paginating a RecyclerView Android

Rakshit Nawani 0 Jan 3, 2022
ANDROID. ChipsLayoutManager (SpanLayoutManager, FlowLayoutManager). A custom layout manager for RecyclerView which mimicric TextView span behaviour, flow layouts behaviour with support of amazing recyclerView features

ChipsLayoutManager This is ChipsLayoutManager - custom Recycler View's LayoutManager which moves item to the next line when no space left on the curre

Oleg Beloy 3.2k Dec 25, 2022
Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Carousel Recyclerview let's you create carousel layout with the power of recyclerview by creating custom layout manager.

Jack and phantom 504 Dec 25, 2022
RecyclerView : SleepQualityTracker with RecyclerView app

RecyclerView - SleepQualityTracker with RecyclerView app SleepQualityTracker with RecyclerView This app builds on the SleepQualityTracker developed pr

Kevin 2 May 14, 2022
ItemDecoration for RecyclerView using LinearLayoutManager for Android

RecyclerItemDecoration RecyclerItemDecoration allows you to draw divider between items in recyclerview with multiple ViewType without considering item

magiepooh 328 Dec 27, 2022
Using RecyclerView to display data instead of ScrollView or lInearLayout for a strong app. It replaces the ScrollView used in trackMySleep app.

RecyclerView - SleepQualityTracker with RecyclerView app This is the toy app for Lesson 7 of the Android App Development in Kotlin course on Udacity.

Espérant GADA 0 Oct 18, 2021