Presenter class. The Presenter class allows us to define the look and feel of our Leanback-enabled app without editing the underlying data structure.
The Presenter Class
The Leanback template we created was built following a custom version of the common development pattern, Model-view-controller (MVC), in which thePresenter class acts as the View. The Presenters are passed to the ArrayObjectAdapter as arguments and define how the content of the Adapter should be displayed.
The Leanback approach provides a variety of predefined Presenters:
CardPresenterdefines singular contentListRowPresenterdefines how various content in a row should be displayed and arrangedDetailsDescriptionPresenterdefines the UI of theDetailsFragment.
ViewHolder pattern and are mostly composed by Custom Views with methods to set the fields of the views.
Let’s take a closer look at the customizing the CardPresenter as an example.
Customizing the CardPresenter
In theBrowseFragment, we see that the CardPresenter defines the UI of a single item of the adapter.
The highlighted view in the previous image is a custom view called ImageCardView and is used in the CardPresenter to define the UI of a single component.
Let’s analyze how views are initialized in the CardPresenter.
- As we mentioned before, the
CardPresenteris built on theViewHolderpattern. This ensures views are correctly recycled and memory is not wasted. - The
CardViewis set toSetFocusable(true)andsetFocusableInTouchMode(true). Although there is no existing touch interaction on a TV screen, this step is necessary because this is how Android currently manages the focus-ability of views.
ImageCardView (and of the Presenter) are set:
cardView is retrieved from the ViewHolder, then we have the simple setters setTitleText() and setContentText(), to edit the main fields of the view.
To retrieve the image for the thumbnail, Leanback automatically uses the Glide library (but you can easily replace it with the image loading library of your choice).

