Presenter
abstract class Presenter : FacetProvider
kotlin.Any | |
↳ | androidx.leanback.widget.Presenter |
A Presenter is used to generate View
s and bind Objects to them on demand. It is closely related to the concept of an , 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 View#findViewById(int)
.
A trivial Presenter that takes a string and renders it into a :
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 | |
---|---|
open |
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. |
abstract |
Base class to perform a task on Presenter. |
Public constructors | |
---|---|
<init>() A Presenter is used to generate |
Public methods | |
---|---|
Any! | |
abstract Unit |
onBindViewHolder(viewHolder: Presenter.ViewHolder!, item: Any!) Binds a |
open Unit |
onBindViewHolder(viewHolder: Presenter.ViewHolder!, item: Any!, payloads: MutableList<Any!>!) Binds a |
abstract Presenter.ViewHolder! |
onCreateViewHolder(parent: ViewGroup!) Creates a new |
abstract Unit |
onUnbindViewHolder(viewHolder: Presenter.ViewHolder!) Unbinds a |
open Unit |
onViewAttachedToWindow(holder: Presenter.ViewHolder!) Called when a view created by this presenter has been attached to a window. |
open Unit |
onViewDetachedFromWindow(holder: Presenter.ViewHolder!) Called when a view created by this presenter has been detached from its window. |
Unit |
Sets dynamic implemented facet in addition to basic Presenter functions. |
open Unit |
setOnClickListener(holder: Presenter.ViewHolder!, listener: OnClickListener!) Called to set a click listener for the given view holder. |
Protected methods | |
---|---|
open static Unit |
cancelAnimationsRecursive(view: View!) Utility method for removing all running animations on a view. |
Public constructors
<init>
Presenter()
A Presenter is used to generate View
s and bind Objects to them on demand. It is closely related to the concept of an , 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 View#findViewById(int)
.
A trivial Presenter that takes a string and renders it into a :
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.Public methods
onBindViewHolder
abstract fun onBindViewHolder(
viewHolder: Presenter.ViewHolder!,
item: Any!
): Unit
Binds a View
to an item.
onBindViewHolder
open fun onBindViewHolder(
viewHolder: Presenter.ViewHolder!,
item: Any!,
payloads: MutableList<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 |
MutableList<Any!>!: A non-null list of merged payloads. Can be empty list if requires full update. |
onCreateViewHolder
abstract fun onCreateViewHolder(parent: ViewGroup!): Presenter.ViewHolder!
Creates a new View
.
onUnbindViewHolder
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
open 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(ViewHolder)
those resources should be restored here.
Parameters | |
---|---|
holder |
Presenter.ViewHolder!: Holder of the view being attached |