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

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