Presenter

public abstract class Presenter
extends Object implements FacetProvider

java.lang.Object
   ↳ androidx.leanback.widget.Presenter


A Presenter is used to generate Views and bind Objects to them on demand. It is closely related to the concept of an RecyclerView.Adapter, but is not position-based. The leanback framework implements the adapter concept using ObjectAdapter which refers to a Presenter (or PresenterSelector) instance.

Presenters should be stateless. Presenters typically extend Presenter.ViewHolder to store all necessary view state information, such as references to child views to be used when binding to avoid expensive calls to View.findViewById(int).

A trivial Presenter that takes a string and renders it into a TextView:

 public class StringTextViewPresenter extends Presenter {
     // This class does not need a custom ViewHolder, since it does not use
     // a complex layout.

     @Override
     public ViewHolder onCreateViewHolder(ViewGroup parent) {
         return new ViewHolder(new TextView(parent.getContext()));
     }

     @Override
     public void onBindViewHolder(ViewHolder viewHolder, Object item) {
         String str = (String) item;
         TextView textView = (TextView) viewHolder.mView;

         textView.setText(item);
     }

     @Override
     public void onUnbindViewHolder(ViewHolder viewHolder) {
         // Nothing to unbind for TextView, but if this viewHolder had
         // allocated bitmaps, they can be released here.
     }
 }
 
In addition to view creation and binding, Presenter allows dynamic interface (facet) to be added: setFacet(Class, Object). Supported facets: ItemAlignmentFacet is used by HorizontalGridView and VerticalGridView to customize child alignment.

Summary

Nested classes

class Presenter.ViewHolder

ViewHolder can be subclassed and used to cache any view accessors needed to improve binding performance (for example, results of findViewById) without needing to subclass a View. 

class Presenter.ViewHolderTask

Base class to perform a task on Presenter.ViewHolder. 

Public constructors

Presenter()

Public methods

final Object getFacet(Class<?> facetClass)

Queries optional implemented facet.

abstract void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item)

Binds a View to an item.

void onBindViewHolder(Presenter.ViewHolder viewHolder, Object item, List<Object> payloads)

Binds a View to an item with a list of payloads.

abstract Presenter.ViewHolder onCreateViewHolder(ViewGroup parent)

Creates a new View.

abstract void onUnbindViewHolder(Presenter.ViewHolder viewHolder)

Unbinds a View from an item.

void onViewAttachedToWindow(Presenter.ViewHolder holder)

Called when a view created by this presenter