Step #2: Adding a Stub ViewHolder

RecyclerView relies upon custom subclasses of RecyclerView.Adapter and RecyclerView.ViewHolder to do “the heavy lifting” of populating its contents. The ViewHolder is responsible for a single item in the RecyclerView, such as a single row in a scrolling list. The Adapter is responsible for creating and populating the ViewHolder instances for each of our model objects, as needed.

So, let’s start by creating a stub subclass of RecyclerView.ViewHolder.

Right-click over the com.commonsware.todo Java package and choose “New” > “Kotlin File/Class” from the context menu. Fill in RosterRowHolder as the “Name” and choose “Class” from the list of Kotlin structures. Then, press Enter or Return to create a stub Kotlin class.

Then, replace the stub with:

package com.commonsware.todo

import androidx.recyclerview.widget.RecyclerView

class RosterRowHolder : RecyclerView.ViewHolder() {
}

This has RosterRowHolder inherit from RecyclerView.ViewHolder.

This will give you an error, complaining that you are not passing a required parameter to the RecyclerView.ViewHolder constructor. We will address that in a later step, so ignore that error for now.


Prev Table of Contents Next

This book is licensed under the Creative Commons Attribution-ShareAlike 4.0 International license.