How to use Recyclerview Adapter in android kotlin Skip to main content

Recent Posts

Top Tourist Places in Rajasthan – A Complete Travel Guide 2025

Top Tourist Places in Rajasthan – A Complete Travel Guide 2025 Introduction Rajasthan , the “ Land of Kings ,” is India’s largest state and a treasure trove of history, culture, and natural beauty. From golden deserts to majestic palaces, serene lakes to vibrant festivals, Rajasthan offers something for every traveller. Whether you love heritage, adventure, or photography, this state will capture your heart. 1. Jaipur – The Pink City Keywords: Jaipur tourism, Jaipur tourist places, Rajasthan capital city attractions Amber Fort : A UNESCO World Heritage Site with stunning architecture and elephant rides. City Palace: A royal complex showcasing Rajput and Mughal designs. Hawa Mahal : The “Palace of Winds” with 953 jharokhas . Jantar Mantar : An astronomical observatory. Shopping at Johari Bazaar & Bapu Bazaar. Best Time to Visit: October to March. 2. Udaipur – The City of Lakes Keywords: Udaipur tourist places, best places in Rajasthan for couples ...

How to use Recyclerview Adapter in android kotlin

How to use Recyclerview Adapter in Android Kotlin

In RecyclerView, there are several built-in functions that you can use to manage and interact with the items in the list. Here is an overview of some of the most commonly used functions:

RecyclerView Adapter Functions

  1. onCreateViewHolder

    • This function is called when a new ViewHolder is created. You inflate the item layout here.

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.note_item, parent, false) return NoteViewHolder(view) }
  2. onBindViewHolder

    • This function is called to display the data at the specified position.

    override fun onBindViewHolder(holder: NoteViewHolder, position: Int) { val note = notesList[position] holder.titleTextView.text = note.title holder.descriptionTextView.text = note.description holder.dateTextView.text = note.date }
  3. getItemCount

    • This function returns the total number of items in the data set held by the adapter.

    override fun getItemCount(): Int { return notesList.size }
  4. getItemId

    • This function returns the stable ID for the item at the given position. You need to override hasStableIds to return true.

    override fun getItemId(position: Int): Long { return notesList[position].id } override fun hasStableIds(): Boolean { return true }

RecyclerView ViewHolder Functions

  1. getAdapterPosition

    • This function returns the position of the ViewHolder in the adapter.

    val position = holder.adapterPosition
  2. getItemViewType

    • This function returns the view type of the item at the specified position.

    override fun getItemViewType(position: Int): Int { return super.getItemViewType(position) }
  3. setIsRecyclable

    • This function sets whether this ViewHolder can be recycled.

    holder.setIsRecyclable(false)

RecyclerView LayoutManager Functions

  1. findViewByPosition

    • This function finds the view corresponding to the given adapter position.

    val view = layoutManager.findViewByPosition(position)
  2. scrollToPosition

    • This function scrolls the RecyclerView to the specified adapter position.

    recyclerView.scrollToPosition(position)
  3. smoothScrollToPosition

    • This function smoothly scrolls the RecyclerView to the specified adapter position.

    recyclerView.smoothScrollToPosition(position)
  4. getChildAt

    • This function returns the child view at the specified index.

    val childView = recyclerView.getChildAt(index)
  5. getPosition

    • This function returns the adapter position of the specified child view.

    val position = recyclerView.getChildAdapterPosition(childView)

Example: Extended RecyclerView Adapter with More Functions

Here's an extended example of a RecyclerView Adapter with more functionalities, including item ID management:


import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import androidx.recyclerview.widget.RecyclerView class NotesAdapter(private val notesList: List<Note>) : RecyclerView.Adapter<NotesAdapter.NoteViewHolder>() { init { setHasStableIds(true) } class NoteViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { val titleTextView: TextView = itemView.findViewById(R.id.titleTextView) val descriptionTextView: TextView = itemView.findViewById(R.id.descriptionTextView) val dateTextView: TextView = itemView.findViewById(R.id.dateTextView) } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): NoteViewHolder { val view = LayoutInflater.from(parent.context).inflate(R.layout.note_item, parent, false) return NoteViewHolder(view) } override fun onBindViewHolder(holder: NoteViewHolder, position: Int) { val note = notesList[position] holder.titleTextView.text = note.title holder.descriptionTextView.text = note.description holder.dateTextView.text = note.date // Example of getting adapter position and setting a click listener holder.itemView.setOnClickListener { val pos = holder.adapterPosition // Handle the click event } } override fun getItemCount(): Int { return notesList.size } override fun getItemId(position: Int): Long { return notesList[position].id } override fun getItemViewType(position: Int): Int { return super.getItemViewType(position) } }

This example includes item click handling and stable ID management. You can add more functionalities and customize them according to your requirements.

Comments