Adding a ViewHolder

The idea behind RecyclerView is that the expensive process of creating views will be avoided as much as possible. Instead, a small set of views is created and they are reused—no space either side—check throughout hence the name RecyclerView—to display information as the user scrolls.

For this to work, we are expected to have a ViewHolder, which is an object that holds the layout elements of the view so that they can be reused later – recycling them. Let's create a ViewHolder inside the adapter, based on the layout that we defined for each item:

class ArticleAdapter {

private val articles: MutableList<Article> = mutableListOf()

class ViewHolder(
val layout: LinearLayout,
val feed: TextView,
val title: TextView,
val summary: TextView
) : RecyclerView.ViewHolder(layout)
}

Notice that our ViewHolder class extends RecyclerView.ViewHolder and is passing the layout itself to the super constructor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.189.180.43