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 (and sometimes references to specific widgets within that view).

Define a view holder by adding an inner class in CrimeListFragment that extends from RecyclerView.ViewHolder.

Listing 9.9  The beginnings of a ViewHolder (CrimeListFragment.kt)

class CrimeListFragment : Fragment() {
    ...
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        ...
    }

    private inner class CrimeHolder(view: View)
        : RecyclerView.ViewHolder(view) {

    }
}

In CrimeHolder’s constructor, you take in the view to hold on to. Immediately, you pass ...

Get Android Programming: The Big Nerd Ranch Guide, 4th 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.