Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1
ViewHolders and Adapters

169

A RecyclerView never creates Views by themselves. It always creates ViewHolders, which bring their
itemViews along for the ride (Figure 8.6).


Figure 8.6  A RecyclerView with its ViewHolders


When the View is simple, ViewHolder has few responsibilities. For more complicated Views, the
ViewHolder makes wiring up the different parts of itemView to a Crime simpler and more efficient.
You will see how this works later on in this chapter, when you build a complex View yourself.


Adapters


Figure 8.6 is somewhat simplified. RecyclerView does not create ViewHolders itself. Instead, it asks
an adapter. An adapter is a controller object that sits between the RecyclerView and the data set that
the RecyclerView should display.


The adapter is responsible for:



  • creating the necessary ViewHolders

  • binding ViewHolders to data from the model layer


To build an adapter, you first define a subclass of RecyclerView.Adapter. Your adapter subclass will
wrap the list of crimes you get from CrimeLab.


http://www.ebook3000.com

Free download pdf