Displaying Results in RecyclerView

For your last task in this chapter, you will switch to the view layer and get PhotoGalleryFragment’s RecyclerView to display some titles.

First, define a ViewHolder class within PhotoGalleryFragment.

Listing 24.33  Adding a ViewHolder implementation (PhotoGalleryFragment.kt)

class PhotoGalleryFragment : Fragment() {
    ...
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        ...
    }

    private class PhotoHolder(itemTextView: TextView)
        : RecyclerView.ViewHolder(itemTextView) {

        val bindTitle: (CharSequence) -> Unit = itemTextView::setText
    }
    ...
}

Next, add a RecyclerView.Adapter to provide PhotoHolders as needed based on a list of GalleryItems.

Listing 24.34  Adding a RecyclerView.Adapter ...

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.