Most visited

Recently visited

Presenter

public abstract class Presenter
extends Object implements FacetProvider

java.lang.Object
   ↳ android.support.v17.leanback.widget.Presenter
Known Direct Subclasses
Known Indirect Subclasses


Presenter用于生成View ,并根据需要将对象绑定到它们。 它与RecyclerView.Adapter的概念密切相关,但不是基于位置的。 包含Leanback框架实现使用适配器概念ObjectAdapter其指的是演示(或PresenterSelector )实例。

演讲者应该是无国籍的。 演示者通常扩展Presenter.ViewHolder以存储所有必要的视图状态信息,例如绑定时要使用的子视图的引用,以避免对findViewById(int)昂贵调用。

一个简单的Presenter,它接受一个字符串并将其呈现为 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可以被子类化并用于缓存任何需要提高绑定性能的视图访问器(例如,findViewById的结果),而不需要继承View的子类。

    class Presenter.ViewHolderTask

    在Presenter.ViewHolder上执行任务的基类。

    Public constructors

    Presenter()

    Public methods

    final Object getFacet(Class<?> facetClass)

    查询可选的已实施构面。

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

    View绑定到一个项目。

    abstract Presenter.ViewHolder onCreateViewHolder(ViewGroup parent)

    创建一个新的 View

    abstract void onUnbindViewHolder(Presenter.ViewHolder viewHolder)

    从项目中 View绑定 View

    void onViewAttachedToWindow(Presenter.ViewHolder holder)

    当此演示者创建的视图已附加到窗口时调用。

    void onViewDetachedFromWindow(Presenter.ViewHolder holder)

    当此演示者创建的视图已从窗口分离时调用。

    final void setFacet(Class<?> facetClass, Object facetImpl)

    除了基本的Presenter功能之外,还设置了动态实施的构面。

    void setOnClickListener(Presenter.ViewHolder holder, View.OnClickListener listener)

    被调用来为给定的视图持有者设置点击监听器。

    Protected methods

    static void cancelAnimationsRecursive(View view)

    用于删除视图上所有正在运行的动画的实用程序方法。

    Inherited methods

    From class java.lang.Object
    From interface android.support.v17.leanback.widget.FacetProvider

    Public constructors

    Presenter

    Presenter ()

    Public methods

    getFacet

    Object getFacet (Class<?> facetClass)

    查询可选的已实施构面。

    Parameters
    facetClass Class: Facet classes to query, examples are: class of ItemAlignmentFacet.
    Returns
    Object Facet implementation for the facetClass or null if feature not implemented.

    onBindViewHolder

    void onBindViewHolder (Presenter.ViewHolder viewHolder, 
                    Object item)

    View绑定到一个项目。

    Parameters
    viewHolder Presenter.ViewHolder
    item Object

    onCreateViewHolder

    Presenter.ViewHolder onCreateViewHolder (ViewGroup parent)

    创建一个新的 View

    Parameters
    parent ViewGroup
    Returns
    Presenter.ViewHolder

    onUnbindViewHolder

    void onUnbindViewHolder (Presenter.ViewHolder viewHolder)

    从物品中View绑定View 任何昂贵的参考资料可能会在此处发布,并且任何未绑定到每个项目的字段都应在此处清除。

    Parameters
    viewHolder Presenter.ViewHolder

    onViewAttachedToWindow

    void onViewAttachedToWindow (Presenter.ViewHolder holder)

    当此演示者创建的视图已附加到窗口时调用。

    这可以用作视图即将被用户看到的合理信号。 如果适配器先前释放了onViewDetachedFromWindow(ViewHolder)任何资源,则应在此恢复这些资源。

    Parameters
    holder Presenter.ViewHolder: Holder of the view being attached

    onViewDetachedFromWindow

    void onViewDetachedFromWindow (Presenter.ViewHolder holder)

    当此演示者创建的视图已从窗口分离时调用。

    脱离窗口不一定是永久的条件; 演示者视图的使用者可以选择在屏幕不可见时缓存屏幕外的视图,并根据需要附加和分离视图。

    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

    void setFacet (Class<?> facetClass, 
                    Object facetImpl)

    除了基本的Presenter功能之外,还设置了动态实施的构面。

    Parameters
    facetClass Class: Facet classes to query, can be class of ItemAlignmentFacet.
    facetImpl Object: Facet implementation.

    setOnClickListener

    void setOnClickListener (Presenter.ViewHolder holder, 
                    View.OnClickListener listener)

    被调用来为给定的视图持有者设置点击监听器。 默认实现在视图持有者的根视图上设置点击监听器。 如果根视图不可聚焦,则应该重写此方法以将侦听器设置在适当的可聚焦子视图上。

    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 methods

    cancelAnimationsRecursive

    void cancelAnimationsRecursive (View view)

    用于删除视图上所有正在运行的动画的实用程序方法。

    Parameters
    view View
  • Hooray!