FileDocCategorySizeDatePackage
RowPresenter.javaAPI DocAndroid 5.1 API19526Thu Mar 12 22:22:56 GMT 2015android.support.v17.leanback.widget

RowPresenter

public abstract class RowPresenter extends Presenter
An abstract {@link Presenter} that renders a {@link Row}.

Customize UI widgets

When a subclass of RowPresenter adds UI widgets, it should subclass {@link RowPresenter.ViewHolder} and override {@link #createRowViewHolder(ViewGroup)} and {@link #initializeRowViewHolder(ViewHolder)}. The subclass must use layout id "row_content" for the widget that will be aligned to the title of any {@link HeadersFragment} that may exist in the parent fragment. RowPresenter contains an optional and replaceable {@link RowHeaderPresenter} that renders the header. You can disable the default rendering or replace the Presenter with a new header presenter by calling {@link #setHeaderPresenter(RowHeaderPresenter)}.

UI events from fragments

RowPresenter receives calls from its parent (typically a Fragment) when:
  • A Row is selected via {@link #setRowViewSelected(Presenter.ViewHolder, boolean)}. The event is triggered immediately when there is a row selection change before the selection animation is started. Subclasses of RowPresenter may override {@link #onRowViewSelected(ViewHolder, boolean)}.
  • A Row is expanded to full width via {@link #setRowViewExpanded(Presenter.ViewHolder, boolean)}. The event is triggered immediately before the expand animation is started. Subclasses of RowPresenter may override {@link #onRowViewExpanded(ViewHolder, boolean)}.

User events

RowPresenter provides {@link OnItemSelectedListener} and {@link OnItemClickedListener}. If a subclass wants to add its own {@link View.OnFocusChangeListener} or {@link View.OnClickListener}, it must do that in {@link #createRowViewHolder(ViewGroup)} to be properly chained by the library. Adding View listeners after {@link #createRowViewHolder(ViewGroup)} is undefined and may result in incorrect behavior by the library's listeners.

Selection animation

When a user scrolls through rows, a fragment will initiate animation and call {@link #setSelectLevel(Presenter.ViewHolder, float)} with float value between 0 and 1. By default, the RowPresenter draws a dim overlay on top of the row view for views that are not selected. Subclasses may override this default effect by having {@link #isUsingDefaultSelectEffect()} return false and overriding {@link #onSelectLevelChanged(ViewHolder)} to apply a different selection effect.

Call {@link #setSelectEffectEnabled(boolean)} to enable/disable the select effect, This will not only enable/disable the default dim effect but also subclasses must respect this flag as well.

Fields Summary
private RowHeaderPresenter
mHeaderPresenter
private OnItemSelectedListener
mOnItemSelectedListener
private OnItemClickedListener
mOnItemClickedListener
private OnItemViewSelectedListener
mOnItemViewSelectedListener
private OnItemViewClickedListener
mOnItemViewClickedListener
boolean
mSelectEffectEnabled
Constructors Summary
Methods Summary
public booleancanDrawOutOfBounds()
Return true if the Row view can draw outside its bounds.

        return false;
    
protected abstract android.support.v17.leanback.widget.RowPresenter$ViewHoldercreateRowViewHolder(android.view.ViewGroup parent)
Called to create a ViewHolder object for a Row. Subclasses will override this method to return a different concrete ViewHolder object.

param
parent The parent View for the Row's view holder.
return
A ViewHolder for the Row's View.

public voidfreeze(android.support.v17.leanback.widget.RowPresenter$ViewHolder holder, boolean freeze)
Freeze/Unfreeze the row, typically used when transition starts/ends. This method is called by fragment, app should not call it directly.

    
public final RowHeaderPresentergetHeaderPresenter()
Get the Presenter used for rendering the header, or null if none has been set.

        return mHeaderPresenter;
    
public final OnItemClickedListenergetOnItemClickedListener()
Get the listener for item click events.

        return mOnItemClickedListener;
    
public final OnItemSelectedListenergetOnItemSelectedListener()
Get the listener for item or row selection.

        return mOnItemSelectedListener;
    
public final OnItemViewClickedListenergetOnItemViewClickedListener()
Set listener for item click event.

        return mOnItemViewClickedListener;
    
public final OnItemViewSelectedListenergetOnItemViewSelectedListener()
Get listener for item or row selection.

        return mOnItemViewSelectedListener;
    
public final android.support.v17.leanback.widget.RowPresenter$ViewHoldergetRowViewHolder(Presenter.ViewHolder holder)
Get the {@link RowPresenter.ViewHolder} from the given Presenter ViewHolder.

        if (holder instanceof ContainerViewHolder) {
            return ((ContainerViewHolder) holder).mRowViewHolder;
        } else {
            return (ViewHolder) holder;
        }
    
public final booleangetSelectEffectEnabled()
Returns true if the row selection effect is enabled. This value not only determines whether the default dim implementation is used, but subclasses must also respect this flag.

        return mSelectEffectEnabled;
    
public final floatgetSelectLevel(Presenter.ViewHolder vh)
Get the current select level. The value will be between 0 (unselected) and 1 (selected).

        return getRowViewHolder(vh).mSelectLevel;
    
protected voidinitializeRowViewHolder(android.support.v17.leanback.widget.RowPresenter$ViewHolder vh)
Called after a {@link RowPresenter.ViewHolder} is created for a Row. Subclasses may override this method and start by calling super.initializeRowViewHolder(ViewHolder).

param
vh The ViewHolder to initialize for the Row.

        vh.mInitialzed = true;
        // set clip children to false for slide transition
        ((ViewGroup) vh.view).setClipChildren(false);
        if (vh.mContainerViewHolder != null) {
            ((ViewGroup) vh.mContainerViewHolder.view).setClipChildren(false);
        }
    
public booleanisUsingDefaultSelectEffect()
Return whether this RowPresenter is using the default dimming effect provided by the library. Subclasses may(most likely) return false and override {@link #onSelectLevelChanged(ViewHolder)}.

        return true;
    
final booleanneedsDefaultSelectEffect()

        return isUsingDefaultSelectEffect() && getSelectEffectEnabled();
    
final booleanneedsRowContainerView()

        return mHeaderPresenter != null || needsDefaultSelectEffect();
    
protected voidonBindRowViewHolder(android.support.v17.leanback.widget.RowPresenter$ViewHolder vh, java.lang.Object item)

        vh.mRow = (Row) item;
        if (vh.mHeaderViewHolder != null) {
            mHeaderPresenter.onBindViewHolder(vh.mHeaderViewHolder, item);
        }
    
public final voidonBindViewHolder(Presenter.ViewHolder viewHolder, java.lang.Object item)

        onBindRowViewHolder(getRowViewHolder(viewHolder), item);
    
public final Presenter.ViewHolderonCreateViewHolder(android.view.ViewGroup parent)


    
         
        ViewHolder vh = createRowViewHolder(parent);
        vh.mInitialzed = false;
        Presenter.ViewHolder result;
        if (needsRowContainerView()) {
            RowContainerView containerView = new RowContainerView(parent.getContext());
            if (mHeaderPresenter != null) {
                vh.mHeaderViewHolder = (RowHeaderPresenter.ViewHolder)
                        mHeaderPresenter.onCreateViewHolder((ViewGroup) vh.view);
            }
            result = new ContainerViewHolder(containerView, vh);
        } else {
            result = vh;
        }
        initializeRowViewHolder(vh);
        if (!vh.mInitialzed) {
            throw new RuntimeException("super.initializeRowViewHolder() must be called");
        }
        return result;
    
protected voidonRowViewAttachedToWindow(android.support.v17.leanback.widget.RowPresenter$ViewHolder vh)

        if (vh.mHeaderViewHolder != null) {
            mHeaderPresenter.onViewAttachedToWindow(vh.mHeaderViewHolder);
        }
    
protected voidonRowViewDetachedFromWindow(android.support.v17.leanback.widget.RowPresenter$ViewHolder vh)

        if (vh.mHeaderViewHolder != null) {
            mHeaderPresenter.onViewDetachedFromWindow(vh.mHeaderViewHolder);
        }
        cancelAnimationsRecursive(vh.view);
    
protected voidonRowViewExpanded(android.support.v17.leanback.widget.RowPresenter$ViewHolder vh, boolean expanded)
Subclass may override this to respond to expanded state changes of a Row. The default implementation will hide/show the header view. Subclasses may make visual changes to the Row View but must not create animation on the Row view.

        updateHeaderViewVisibility(vh);
        vh.view.setActivated(expanded);
    
protected voidonRowViewSelected(android.support.v17.leanback.widget.RowPresenter$ViewHolder vh, boolean selected)
Subclass may override this to respond to selected state changes of a Row. Subclass may make visual changes to Row view but must not create animation on the Row view.

        if (selected) {
            if (mOnItemViewSelectedListener != null) {
                mOnItemViewSelectedListener.onItemSelected(null, null, vh, vh.getRow());
            }
            if (mOnItemSelectedListener != null) {
                mOnItemSelectedListener.onItemSelected(null, vh.getRow());
            }
        }
        updateHeaderViewVisibility(vh);
    
protected voidonSelectLevelChanged(android.support.v17.leanback.widget.RowPresenter$ViewHolder vh)
Callback when select level is changed. The default implementation applies the select level to {@link RowHeaderPresenter#setSelectLevel(RowHeaderPresenter.ViewHolder, float)} when {@link #getSelectEffectEnabled()} is true. Subclasses may override this function and implement a different select effect. In this case, you should also override {@link #isUsingDefaultSelectEffect()} to disable the default dimming effect applied by the library.

        if (getSelectEffectEnabled()) {
            vh.mColorDimmer.setActiveLevel(vh.mSelectLevel);
            if (vh.mHeaderViewHolder != null) {
                mHeaderPresenter.setSelectLevel(vh.mHeaderViewHolder, vh.mSelectLevel);
            }
            if (isUsingDefaultSelectEffect()) {
                ((RowContainerView) vh.mContainerViewHolder.view).setForegroundColor(
                        vh.mColorDimmer.getPaint().getColor());
            }
        }
    
protected voidonUnbindRowViewHolder(android.support.v17.leanback.widget.RowPresenter$ViewHolder vh)

        if (vh.mHeaderViewHolder != null) {
            mHeaderPresenter.onUnbindViewHolder(vh.mHeaderViewHolder);
        }
        vh.mRow = null;
    
public final voidonUnbindViewHolder(Presenter.ViewHolder viewHolder)

        onUnbindRowViewHolder(getRowViewHolder(viewHolder));
    
public final voidonViewAttachedToWindow(Presenter.ViewHolder holder)

        onRowViewAttachedToWindow(getRowViewHolder(holder));
    
public final voidonViewDetachedFromWindow(Presenter.ViewHolder holder)

        onRowViewDetachedFromWindow(getRowViewHolder(holder));
    
public voidsetEntranceTransitionState(android.support.v17.leanback.widget.RowPresenter$ViewHolder holder, boolean afterTransition)
Change visibility of views, entrance transition will be run against the views that change visibilities. Subclass may override and begin with calling super.setEntranceTransitionState(). This method is called by fragment, app should not call it directly.

        if (holder.mHeaderViewHolder != null) {
            holder.mHeaderViewHolder.view.setVisibility(afterTransition ?
                    View.VISIBLE : View.INVISIBLE);
        }
    
public final voidsetHeaderPresenter(RowHeaderPresenter headerPresenter)
Set the Presenter used for rendering the header. Can be null to disable header rendering. The method must be called before creating any Row Views.

        mHeaderPresenter = headerPresenter;
    
public final voidsetOnItemClickedListener(OnItemClickedListener listener)
Set the listener for item click events. A RowPresenter does not use this listener, but a subclass may fire an item click event if it has the concept of an item. The {@link OnItemClickedListener} will override any {@link View.OnClickListener} that an item's Presenter sets during {@link Presenter#onCreateViewHolder(ViewGroup)}. So in general, you should choose to use an OnItemClickedListener or a {@link View.OnClickListener}, but not both.

        mOnItemClickedListener = listener;
    
public final voidsetOnItemSelectedListener(OnItemSelectedListener listener)
Set the listener for item or row selection. A RowPresenter fires a row selection event with a null item. Subclasses (e.g. {@link ListRowPresenter}) can fire a selection event with the selected item.

        mOnItemSelectedListener = listener;
    
public final voidsetOnItemViewClickedListener(OnItemViewClickedListener listener)
Set listener for item click event. RowPresenter does nothing but subclass of RowPresenter may fire item click event if it does have a concept of item. OnItemViewClickedListener will override {@link View.OnClickListener} that item presenter sets during {@link Presenter#onCreateViewHolder(ViewGroup)}. So in general, developer should choose one of the listeners but not both.

        mOnItemViewClickedListener = listener;
    
public final voidsetOnItemViewSelectedListener(OnItemViewSelectedListener listener)
Set listener for item or row selection. RowPresenter fires row selection event with null item, subclass of RowPresenter e.g. {@link ListRowPresenter} can fire a selection event with selected item.

        mOnItemViewSelectedListener = listener;
    
public final voidsetRowViewExpanded(Presenter.ViewHolder holder, boolean expanded)
Set the expanded state of a Row view.

param
holder The Row ViewHolder to set expanded state on.
param
expanded True if the Row is expanded, false otherwise.

        ViewHolder rowViewHolder = getRowViewHolder(holder);
        rowViewHolder.mExpanded = expanded;
        onRowViewExpanded(rowViewHolder, expanded);
    
public final voidsetRowViewSelected(Presenter.ViewHolder holder, boolean selected)
Set the selected state of a Row view.

param
holder The Row ViewHolder to set expanded state on.
param
selected True if the Row is expanded, false otherwise.

        ViewHolder rowViewHolder = getRowViewHolder(holder);
        rowViewHolder.mSelected = selected;
        onRowViewSelected(rowViewHolder, selected);
    
public final voidsetSelectEffectEnabled(boolean applyDimOnSelect)
Enables or disables the row selection effect. This will not only affect the default dim effect, but subclasses must respect this flag as well.

        mSelectEffectEnabled = applyDimOnSelect;
    
public final voidsetSelectLevel(Presenter.ViewHolder vh, float level)
Set the current select level to a value between 0 (unselected) and 1 (selected). Subclasses may override {@link #onSelectLevelChanged(ViewHolder)} to respond to changes in the selected level.

        ViewHolder rowViewHolder = getRowViewHolder(vh);
        rowViewHolder.mSelectLevel = level;
        onSelectLevelChanged(rowViewHolder);
    
private voidupdateHeaderViewVisibility(android.support.v17.leanback.widget.RowPresenter$ViewHolder vh)

        if (mHeaderPresenter != null && vh.mHeaderViewHolder != null) {
            RowContainerView containerView = ((RowContainerView) vh.mContainerViewHolder.view);
            containerView.showHeader(vh.isExpanded());
        }