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 O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.