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

ListRowPresenter

public class ListRowPresenter extends RowPresenter
ListRowPresenter renders {@link ListRow} using a {@link HorizontalGridView} hosted in a {@link ListRowView}.

Hover card

Optionally, {@link #setHoverCardPresenterSelector(PresenterSelector)} can be used to display a view for the currently focused list item below the rendered list. This view is known as a hover card.

Selection animation

ListRowPresenter disables {@link RowPresenter}'s default dimming effect and draw a dim overlay on top of each individual child items. Subclass may override and disable {@link #isUsingDefaultListSelectEffect()} and write its own dim effect in {@link #onSelectLevelChanged(RowPresenter.ViewHolder)}.

Shadow

ListRowPresenter applies a default shadow to child of each view. Call {@link #setShadowEnabled(boolean)} to disable shadow. Subclass may override and return false in {@link #isUsingDefaultShadow()} and replace with its own shadow implementation.

Fields Summary
private static final String
TAG
private static final boolean
DEBUG
private static final int
DEFAULT_RECYCLED_POOL_SIZE
private int
mRowHeight
private int
mExpandedRowHeight
private PresenterSelector
mHoverCardPresenterSelector
private int
mZoomFactor
private boolean
mShadowEnabled
private int
mBrowseRowsFadingEdgeLength
private boolean
mRoundedCornersEnabled
private HashMap
mRecycledPoolSize
private static int
sSelectedRowTopPadding
private static int
sExpandedSelectedRowTopPadding
private static int
sExpandedRowNoHovercardBottomPadding
private ItemBridgeAdapter.Wrapper
mCardWrapper
Constructors Summary
public ListRowPresenter()
Constructs a ListRowPresenter with defaults. Uses {@link FocusHighlight#ZOOM_FACTOR_MEDIUM} for focus zooming.


                    
      
        this(FocusHighlight.ZOOM_FACTOR_MEDIUM);
    
public ListRowPresenter(int zoomFactor)
Constructs a ListRowPresenter with the given parameters.

param
zoomFactor Controls the zoom factor used when an item view is focused. One of {@link FocusHighlight#ZOOM_FACTOR_NONE}, {@link FocusHighlight#ZOOM_FACTOR_SMALL}, {@link FocusHighlight#ZOOM_FACTOR_MEDIUM}, {@link FocusHighlight#ZOOM_FACTOR_LARGE}

        mZoomFactor = zoomFactor;
    
Methods Summary
public final booleanareChildRoundedCornersEnabled()
Returns true if rounded corners are enabled for children of this row.

        return mRoundedCornersEnabled;
    
public booleancanDrawOutOfBounds()

        return needsDefaultShadow();
    
protected android.support.v17.leanback.widget.RowPresenter.ViewHoldercreateRowViewHolder(android.view.ViewGroup parent)

        initStatics(parent.getContext());
        ListRowView rowView = new ListRowView(parent.getContext());
        setupFadingEffect(rowView);
        if (mRowHeight != 0) {
            rowView.getGridView().setRowHeight(mRowHeight);
        }
        return new ViewHolder(rowView, rowView.getGridView(), this);
    
public final voidenableChildRoundedCorners(boolean enable)
Enables or disabled rounded corners on children of this row. Supported on Android SDK >= L.

        mRoundedCornersEnabled = enable;
    
public voidfreeze(android.support.v17.leanback.widget.RowPresenter.ViewHolder holder, boolean freeze)

        ViewHolder vh = (ViewHolder) holder;
        vh.mGridView.setScrollEnabled(!freeze);
    
public intgetExpandedRowHeight()
Returns the expanded row height for rows created by this Presenter.

        return mExpandedRowHeight != 0 ? mExpandedRowHeight : mRowHeight;
    
public final PresenterSelectorgetHoverCardPresenterSelector()
Get {@link PresenterSelector} used for showing a select object in a hover card.

        return mHoverCardPresenterSelector;
    
public intgetRecycledPoolSize(Presenter presenter)
Returns the recycled pool size for the given presenter.

        return mRecycledPoolSize.containsKey(presenter) ? mRecycledPoolSize.get(presenter) :
                DEFAULT_RECYCLED_POOL_SIZE;
    
public intgetRowHeight()
Returns the row height for list rows created by this Presenter.

        return mRowHeight;
    
public final booleangetShadowEnabled()
Returns true if child shadow is enabled. This is not only for enable/disable default shadow implementation but also subclass must respect this flag.

        return mShadowEnabled;
    
private intgetSpaceUnderBaseline(android.support.v17.leanback.widget.ListRowPresenter$ViewHolder vh)

        RowHeaderPresenter.ViewHolder headerViewHolder = vh.getHeaderViewHolder();
        if (headerViewHolder != null) {
            if (getHeaderPresenter() != null) {
                return getHeaderPresenter().getSpaceUnderBaseline(headerViewHolder);
            }
            return headerViewHolder.view.getPaddingBottom();
        }
        return 0;
    
public final intgetZoomFactor()
Returns the zoom factor used for focus highlighting.

        return mZoomFactor;
    
private static voidinitStatics(android.content.Context context)

        if (sSelectedRowTopPadding == 0) {
            sSelectedRowTopPadding = context.getResources().getDimensionPixelSize(
                    R.dimen.lb_browse_selected_row_top_padding);
            sExpandedSelectedRowTopPadding = context.getResources().getDimensionPixelSize(
                    R.dimen.lb_browse_expanded_selected_row_top_padding);
            sExpandedRowNoHovercardBottomPadding = context.getResources().getDimensionPixelSize(
                    R.dimen.lb_browse_expanded_row_no_hovercard_bottom_padding);
        }
    
protected voidinitializeRowViewHolder(android.support.v17.leanback.widget.RowPresenter.ViewHolder holder)


    
        
        super.initializeRowViewHolder(holder);
        final ViewHolder rowViewHolder = (ViewHolder) holder;
        if (needsDefaultListSelectEffect() || needsDefaultShadow()
                || areChildRoundedCornersEnabled()) {
            rowViewHolder.mItemBridgeAdapter.setWrapper(mCardWrapper);
        }
        if (needsDefaultListSelectEffect()) {
            ShadowOverlayContainer.prepareParentForShadow(rowViewHolder.mGridView);
        }
        FocusHighlightHelper.setupBrowseItemFocusHighlight(rowViewHolder.mItemBridgeAdapter,
                mZoomFactor, false);
        rowViewHolder.mGridView.setFocusDrawingOrderEnabled(!isUsingZOrder());
        rowViewHolder.mGridView.setOnChildSelectedListener(
                new OnChildSelectedListener() {
            @Override
            public void onChildSelected(ViewGroup parent, View view, int position, long id) {
                selectChildView(rowViewHolder, view);
            }
        });
        rowViewHolder.mItemBridgeAdapter.setAdapterListener(
                new ItemBridgeAdapter.AdapterListener() {
            @Override
            public void onBind(final ItemBridgeAdapter.ViewHolder viewHolder) {
                // Only when having an OnItemClickListner, we will attach the OnClickListener.
                if (getOnItemClickedListener() != null || getOnItemViewClickedListener() != null) {
                    viewHolder.mHolder.view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            ItemBridgeAdapter.ViewHolder ibh = (ItemBridgeAdapter.ViewHolder)
                                    rowViewHolder.mGridView
                                            .getChildViewHolder(viewHolder.itemView);
                            if (getOnItemClickedListener() != null) {
                                getOnItemClickedListener().onItemClicked(ibh.mItem,
                                        (ListRow) rowViewHolder.mRow);
                            }
                            if (getOnItemViewClickedListener() != null) {
                                getOnItemViewClickedListener().onItemClicked(viewHolder.mHolder,
                                        ibh.mItem, rowViewHolder, (ListRow) rowViewHolder.mRow);
                            }
                        }
                    });
                }
            }

            @Override
            public void onUnbind(ItemBridgeAdapter.ViewHolder viewHolder) {
                if (getOnItemClickedListener() != null || getOnItemViewClickedListener() != null) {
                    viewHolder.mHolder.view.setOnClickListener(null);
                }
            }

            @Override
            public void onAttachedToWindow(ItemBridgeAdapter.ViewHolder viewHolder) {
                if (viewHolder.itemView instanceof ShadowOverlayContainer) {
                    int dimmedColor = rowViewHolder.mColorDimmer.getPaint().getColor();
                    ((ShadowOverlayContainer) viewHolder.itemView).setOverlayColor(dimmedColor);
                }
                viewHolder.itemView.setActivated(rowViewHolder.mExpanded);
            }

            @Override
            public void onAddPresenter(Presenter presenter, int type) {
                rowViewHolder.getGridView().getRecycledViewPool().setMaxRecycledViews(
                        type, getRecycledPoolSize(presenter));
            }
        });
    
public booleanisUsingDefaultListSelectEffect()
Returns true so that default select effect is applied to each individual child of {@link HorizontalGridView}. Subclass may return false to disable the default implementation.

see
#onSelectLevelChanged(RowPresenter.ViewHolder)

        return true;
    
public final booleanisUsingDefaultSelectEffect()
ListRowPresenter overrides the default select effect of {@link RowPresenter} and return false.

        return false;
    
public booleanisUsingDefaultShadow()
Returns true if SDK >= 18, where default shadow is applied to each individual child of {@link HorizontalGridView}. Subclass may return false to disable.

        return ShadowOverlayContainer.supportsShadow();
    
public booleanisUsingZOrder()
Returns true if SDK >= L, where Z shadow is enabled so that Z order is enabled on each child of horizontal list. If subclass returns false in isUsingDefaultShadow() and does not use Z-shadow on SDK >= L, it should override isUsingZOrder() return false.

        return ShadowHelper.getInstance().usesZShadow();
    
final booleanneedsDefaultListSelectEffect()

        return isUsingDefaultListSelectEffect() && getSelectEffectEnabled();
    
final booleanneedsDefaultShadow()

        return isUsingDefaultShadow() && getShadowEnabled();
    
protected voidonBindRowViewHolder(android.support.v17.leanback.widget.RowPresenter.ViewHolder holder, java.lang.Object item)

        super.onBindRowViewHolder(holder, item);
        ViewHolder vh = (ViewHolder) holder;
        ListRow rowItem = (ListRow) item;
        vh.mItemBridgeAdapter.setAdapter(rowItem.getAdapter());
        vh.mGridView.setAdapter(vh.mItemBridgeAdapter);
    
protected voidonRowViewExpanded(android.support.v17.leanback.widget.RowPresenter.ViewHolder holder, boolean expanded)

        super.onRowViewExpanded(holder, expanded);
        ViewHolder vh = (ViewHolder) holder;
        if (getRowHeight() != getExpandedRowHeight()) {
            int newHeight = expanded ? getExpandedRowHeight() : getRowHeight();
            vh.getGridView().setRowHeight(newHeight);
        }
        setVerticalPadding(vh);
        updateFooterViewSwitcher(vh);
    
protected voidonRowViewSelected(android.support.v17.leanback.widget.RowPresenter.ViewHolder holder, boolean selected)

        super.onRowViewSelected(holder, selected);
        ViewHolder vh = (ViewHolder) holder;
        setVerticalPadding(vh);
        updateFooterViewSwitcher(vh);
    
protected voidonSelectLevelChanged(android.support.v17.leanback.widget.RowPresenter.ViewHolder holder)
Applies select level to header and draw a default color dim over each child of {@link HorizontalGridView}.

Subclass may override this method. A subclass needs to call super.onSelectLevelChanged() for applying header select level and optionally applying a default select level to each child view of {@link HorizontalGridView} if {@link #isUsingDefaultListSelectEffect()} is true. Subclass may override {@link #isUsingDefaultListSelectEffect()} to return false and deal with the individual item select level by itself.

        super.onSelectLevelChanged(holder);
        if (needsDefaultListSelectEffect()) {
            ViewHolder vh = (ViewHolder) holder;
            int dimmedColor = vh.mColorDimmer.getPaint().getColor();
            for (int i = 0, count = vh.mGridView.getChildCount(); i < count; i++) {
                ShadowOverlayContainer wrapper = (ShadowOverlayContainer) vh.mGridView.getChildAt(i);
                wrapper.setOverlayColor(dimmedColor);
            }
            if (vh.mGridView.getFadingLeftEdge()) {
                vh.mGridView.invalidate();
            }
        }
    
protected voidonUnbindRowViewHolder(android.support.v17.leanback.widget.RowPresenter.ViewHolder holder)

        ViewHolder vh = (ViewHolder) holder;
        vh.mGridView.setAdapter(null);
        vh.mItemBridgeAdapter.clear();
        super.onUnbindRowViewHolder(holder);
    
private voidselectChildView(android.support.v17.leanback.widget.ListRowPresenter$ViewHolder rowViewHolder, android.view.View view)

        ItemBridgeAdapter.ViewHolder ibh = null;
        if (view != null) {
            ibh = (ItemBridgeAdapter.ViewHolder)
                    rowViewHolder.mGridView.getChildViewHolder(view);
        }
        if (view == null) {
            if (mHoverCardPresenterSelector != null) {
                rowViewHolder.mHoverCardViewSwitcher.unselect();
            }
            if (getOnItemViewSelectedListener() != null) {
                getOnItemViewSelectedListener().onItemSelected(null, null,
                        rowViewHolder, rowViewHolder.mRow);
            }
            if (getOnItemSelectedListener() != null) {
                getOnItemSelectedListener().onItemSelected(null, rowViewHolder.mRow);
            }
        } else if (rowViewHolder.mExpanded && rowViewHolder.mSelected) {
            if (mHoverCardPresenterSelector != null) {
                rowViewHolder.mHoverCardViewSwitcher.select(rowViewHolder.mGridView, view,
                        ibh.mItem);
            }
            if (getOnItemViewSelectedListener() != null) {
                getOnItemViewSelectedListener().onItemSelected(ibh.mHolder, ibh.mItem,
                        rowViewHolder, rowViewHolder.mRow);
            }
            if (getOnItemSelectedListener() != null) {
                getOnItemSelectedListener().onItemSelected(ibh.mItem, rowViewHolder.mRow);
            }
        }
    
public voidsetEntranceTransitionState(android.support.v17.leanback.widget.RowPresenter.ViewHolder holder, boolean afterEntrance)

        super.setEntranceTransitionState(holder, afterEntrance);
        ((ViewHolder) holder).mGridView.setChildrenVisibility(
                afterEntrance? View.VISIBLE : View.INVISIBLE);
    
public voidsetExpandedRowHeight(int rowHeight)
Sets the expanded row height for rows created by this Presenter. If not set, expanded rows have the same height as unexpanded rows.

param
rowHeight The row height in to use when the row is expanded, in pixels, or WRAP_CONTENT, or 0 to use the default.

        mExpandedRowHeight = rowHeight;
    
public final voidsetHoverCardPresenterSelector(PresenterSelector selector)
Set {@link PresenterSelector} used for showing a select object in a hover card.

        mHoverCardPresenterSelector = selector;
    
public voidsetRecycledPoolSize(Presenter presenter, int size)
Sets the recycled pool size for the given presenter.

        mRecycledPoolSize.put(presenter, size);
    
public voidsetRowHeight(int rowHeight)
Sets the row height for rows created by this Presenter. Rows created before calling this method will not be updated.

param
rowHeight Row height in pixels, or WRAP_CONTENT, or 0 to use the default height.

        mRowHeight = rowHeight;
    
public final voidsetShadowEnabled(boolean enabled)
Enable or disable child shadow. This is not only for enable/disable default shadow implementation but also subclass must respect this flag.

        mShadowEnabled = enabled;
    
private voidsetVerticalPadding(android.support.v17.leanback.widget.ListRowPresenter$ViewHolder vh)

        int paddingTop, paddingBottom;
        // Note: sufficient bottom padding needed for card shadows.
        if (vh.isExpanded()) {
            int headerSpaceUnderBaseline = getSpaceUnderBaseline(vh);
            if (DEBUG) Log.v(TAG, "headerSpaceUnderBaseline " + headerSpaceUnderBaseline);
            paddingTop = (vh.isSelected() ? sExpandedSelectedRowTopPadding : vh.mPaddingTop) -
                    headerSpaceUnderBaseline;
            paddingBottom = mHoverCardPresenterSelector == null ?
                    sExpandedRowNoHovercardBottomPadding : vh.mPaddingBottom;
        } else if (vh.isSelected()) {
            paddingTop = sSelectedRowTopPadding - vh.mPaddingBottom;
            paddingBottom = sSelectedRowTopPadding;
        } else {
            paddingTop = 0;
            paddingBottom = vh.mPaddingBottom;
        }
        vh.getGridView().setPadding(vh.mPaddingLeft, paddingTop, vh.mPaddingRight,
                paddingBottom);
    
private voidsetupFadingEffect(ListRowView rowView)

        // content is completely faded at 1/2 padding of left, fading length is 1/2 of padding.
        HorizontalGridView gridView = rowView.getGridView();
        if (mBrowseRowsFadingEdgeLength < 0) {
            TypedArray ta = gridView.getContext()
                    .obtainStyledAttributes(R.styleable.LeanbackTheme);
            mBrowseRowsFadingEdgeLength = (int) ta.getDimension(
                    R.styleable.LeanbackTheme_browseRowsFadingEdgeLength, 0);
            ta.recycle();
        }
        gridView.setFadingLeftEdgeLength(mBrowseRowsFadingEdgeLength);
    
private voidupdateFooterViewSwitcher(android.support.v17.leanback.widget.ListRowPresenter$ViewHolder vh)

        if (vh.mExpanded && vh.mSelected) {
            if (mHoverCardPresenterSelector != null) {
                vh.mHoverCardViewSwitcher.init((ViewGroup) vh.view,
                        mHoverCardPresenterSelector);
            }
            ItemBridgeAdapter.ViewHolder ibh = (ItemBridgeAdapter.ViewHolder)
                    vh.mGridView.findViewHolderForPosition(
                            vh.mGridView.getSelectedPosition());
            selectChildView(vh, ibh == null ? null : ibh.itemView);
        } else {
            if (mHoverCardPresenterSelector != null) {
                vh.mHoverCardViewSwitcher.unselect();
            }
        }