Implementing a ViewHolder

The RecyclerView expects an item view to be wrapped in an instance of ViewHolder. A ViewHolder stores a reference to an item’s view. But, as usual, you are not going to interact directly with the View. You are going to use View Binding.

Create a new file named CrimeListAdapter.kt. In it, define a view holder by adding a CrimeHolder class that extends from RecyclerView.ViewHolder.

Listing 10.10  The beginnings of a ViewHolder (CrimeListAdapter.kt)

class CrimeHolder(
    val binding: ListItemCrimeBinding
) : RecyclerView.ViewHolder(binding.root) {

}

In CrimeHolder’s constructor, you take in the binding to hold on to. Immediately, you pass its root view as the argument to the RecyclerView.ViewHolder

Get Android Programming: The Big Nerd Ranch Guide, 5th Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.