Presenter


abstract class Presenter : FacetProvider

Known direct subclasses
AbstractDetailsDescriptionPresenter

An abstract Presenter for rendering a detailed description of an item.

DetailsOverviewLogoPresenter

Presenter that responsible to create a ImageView and bind to DetailsOverviewRow.

DividerPresenter

DividerPresenter provides a default presentation for DividerRow in HeadersFragment.

RowHeaderPresenter

RowHeaderPresenter provides a default presentation for HeaderItem using a RowHeaderView and optionally a TextView for description.

RowPresenter

An abstract Presenter that renders an Object in RowsFragment, the object can be subclass Row or a generic one.

VerticalGridPresenter

A presenter that renders objects in a VerticalGridView.

Known indirect subclasses
AbstractMediaItemPresenter

Abstract Presenter class for rendering media items in a playlist format.

AbstractMediaListHeaderPresenter

Abstract presenter class for rendering the header for a list of media items in a playlist.

DetailsOverviewRowPresenter

This class is deprecated.

Use FullWidthDetailsOverviewRowPresenter

FullWidthDetailsOverviewRowPresenter

Renders a DetailsOverviewRow to display an overview of an item.

ListRowPresenter

ListRowPresenter renders ListRow using a HorizontalGridView hosted in a ListRowView.

PlaybackControlsRowPresenter

A PlaybackControlsRowPresenter renders a PlaybackControlsRow to display a series of playback control buttons.

PlaybackRowPresenter

Subclass of RowPresenter that can define the desired behavior when the view reappears.

PlaybackTransportRowPresenter

A PlaybackTransportRowPresenter renders a PlaybackControlsRow to display a series of playback control buttons.


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 ViewHolder to store all necessary view state information, such as references to child views to be used when binding to avoid expensive calls to findViewById.

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. Supported facets: ItemAlignmentFacet is used by HorizontalGridView and VerticalGridView to customize child alignment.

Summary

Nested types

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.

Base class to perform a task on Presenter.ViewHolder.

Public constructors

Public functions

Any!
getFacet(facetClass: Class<Any!>!)

Queries optional implemented facet.

abstract Unit
onBindViewHolder(viewHolder: Presenter.ViewHolder, item: Any?)

Binds a View to an item.

Unit
onBindViewHolder(
    viewHolder: Presenter.ViewHolder,
    item: Any,
    payloads: (Mutable)List<Any!>
)

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

abstract Presenter.ViewHolder

Creates a new View.

abstract Unit

Unbinds a View from an item.

Unit

Called when a view created by this presenter has been attached to a window.

Unit

Called when a view created by this presenter has been detached from its window.

Unit
setFacet(facetClass: Class<Any!>!, facetImpl: Any!)

Sets dynamic implemented facet in addition to basic Presenter functions.

Unit
setOnClickListener(
    holder: Presenter.ViewHolder!,
    listener: View.OnClickListener!
)

Called to set a click listener for the given view holder.

Protected functions

java-static Unit

Utility method for removing all running animations on a view.

Public constructors

Presenter

Added in 1.1.0
Presenter()

Public functions

getFacet

Added in 1.2.0-alpha04
fun getFacet(facetClass: Class<Any!>!): Any!

Queries optional implemented facet.

Parameters
facetClass: Class<Any!>!

Facet classes to query, examples are: class of ItemAlignmentFacet.

Returns
Any!

Facet implementation for the facetClass or null if feature not implemented.

onBindViewHolder

Added in 1.1.0
abstract fun onBindViewHolder(viewHolder: Presenter.ViewHolder, item: Any?): Unit

Binds a View to an item.

onBindViewHolder

Added in 1.1.0
fun onBindViewHolder(
    viewHolder: Presenter.ViewHolder,
    item: Any,
    payloads: (Mutable)List<Any!>
): Unit

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

Parameters
viewHolder: Presenter.ViewHolder

The ViewHolder which should be updated to represent the contents of the item at the given position in the data set.

item: Any

The item which should be bound to view holder.

payloads: (Mutable)List<Any!>

A non-null list of merged payloads. Can be empty list if requires full update.

onCreateViewHolder

Added in 1.1.0
abstract fun onCreateViewHolder(parent: ViewGroup): Presenter.ViewHolder

Creates a new View.

onUnbindViewHolder

Added in 1.1.0
abstract fun onUnbindViewHolder(viewHolder: Presenter.ViewHolder): Unit

Unbinds a View from an item. Any expensive references may be released here, and any fields that are not bound for every item should be cleared here.

onViewAttachedToWindow

Added in 1.1.0
fun onViewAttachedToWindow(holder: Presenter.ViewHolder): Unit

Called when a view created by this presenter has been attached to a window.

This can be used as a reasonable signal that the view is about to be seen by the user. If the adapter previously freed any resources in onViewDetachedFromWindow those resources should be restored here.

Parameters
holder: Presenter.ViewHolder

Holder of the view being attached

onViewDetachedFromWindow

Added in 1.1.0
fun onViewDetachedFromWindow(holder: Presenter.ViewHolder): Unit

Called when a view created by this presenter has been detached from its window.

Becoming detached from the window is not necessarily a permanent condition; the consumer of an presenter's views may choose to cache views offscreen while they are not visible, attaching and detaching them as appropriate.

Any view property animations should be cancelled here or the view may fail to be recycled.
Parameters
holder: Presenter.ViewHolder

Holder of the view being detached

setFacet

Added in 1.1.0
fun setFacet(facetClass: Class<Any!>!, facetImpl: Any!): Unit

Sets dynamic implemented facet in addition to basic Presenter functions.

Parameters
facetClass: Class<Any!>!

Facet classes to query, can be class of ItemAlignmentFacet.

facetImpl: Any!

Facet implementation.

setOnClickListener

Added in 1.1.0
fun setOnClickListener(
    holder: Presenter.ViewHolder!,
    listener: View.OnClickListener!
): Unit

Called to set a click listener for the given view holder. The default implementation sets the click listener on the root view in the view holder. If the root view isn't focusable this method should be overridden to set the listener on the appropriate focusable child view(s).

Parameters
holder: Presenter.ViewHolder!

The view holder containing the view(s) on which the listener should be set.

listener: View.OnClickListener!

The click listener to be set.

Protected functions

cancelAnimationsRecursive

Added in 1.1.0
protected java-static fun cancelAnimationsRecursive(view: View!): Unit

Utility method for removing all running animations on a view.