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
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) }
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 }
getItemCount
- This function returns the total number of items in the data set held by the adapter.
override fun getItemCount(): Int { return notesList.size }
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 }
- This function returns the stable ID for the item at the given position. You need to override
RecyclerView ViewHolder Functions
getAdapterPosition
- This function returns the position of the ViewHolder in the adapter.
val position = holder.adapterPosition
getItemViewType
- This function returns the view type of the item at the specified position.
override fun getItemViewType(position: Int): Int { return super.getItemViewType(position) }
setIsRecyclable
- This function sets whether this ViewHolder can be recycled.
holder.setIsRecyclable(false)
RecyclerView LayoutManager Functions
findViewByPosition
- This function finds the view corresponding to the given adapter position.
val view = layoutManager.findViewByPosition(position)
scrollToPosition
- This function scrolls the RecyclerView to the specified adapter position.
recyclerView.scrollToPosition(position)
smoothScrollToPosition
- This function smoothly scrolls the RecyclerView to the specified adapter position.
recyclerView.smoothScrollToPosition(position)
getChildAt
- This function returns the child view at the specified index.
val childView = recyclerView.getChildAt(index)
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)
}
}
Comments
Post a Comment