public class

VerticalGridPresenter

extends Presenter

 java.lang.Object

androidx.leanback.widget.Presenter

↳androidx.leanback.widget.VerticalGridPresenter

Gradle dependencies

compile group: 'androidx.leanback', name: 'leanback', version: '1.2.0-alpha04'

  • groupId: androidx.leanback
  • artifactId: leanback
  • version: 1.2.0-alpha04

Artifact androidx.leanback:leanback:1.2.0-alpha04 it located at Google repository (https://maven.google.com/)

Androidx artifact mapping:

androidx.leanback:leanback com.android.support:leanback-v17

Androidx class mapping:

androidx.leanback.widget.VerticalGridPresenter android.support.v17.leanback.widget.VerticalGridPresenter

Overview

A presenter that renders objects in a VerticalGridView.

Summary

Constructors
publicVerticalGridPresenter()

Constructs a VerticalGridPresenter with defaults.

publicVerticalGridPresenter(int focusZoomFactor)

Constructs a VerticalGridPresenter with the given parameters.

publicVerticalGridPresenter(int focusZoomFactor, boolean useFocusDimmer)

Constructs a VerticalGridPresenter with the given parameters.

Methods
public final booleanareChildRoundedCornersEnabled()

Returns true if rounded corners are enabled for children of this row.

protected VerticalGridPresenter.ViewHoldercreateGridViewHolder(ViewGroup parent)

Subclass may override this to inflate a different layout.

protected ShadowOverlayHelper.OptionscreateShadowOverlayOptions()

Create ShadowOverlayHelper Options.

public final voidenableChildRoundedCorners(boolean enable)

Enables or disabled rounded corners on children of this row.

public final intgetFocusZoomFactor()

Returns the zoom factor used for focus highlighting.

public final booleangetKeepChildForeground()

Returns true if keeps foreground of child of this grid, the foreground will not be used for overlay color.

public intgetNumberOfColumns()

Returns the number of columns in the vertical grid.

public final OnItemViewClickedListenergetOnItemViewClickedListener()

Returns the item clicked listener.

public final OnItemViewSelectedListenergetOnItemViewSelectedListener()

Returns the item selected listener.

public final booleangetShadowEnabled()

Returns true if child shadow is enabled.

protected voidinitializeGridViewHolder(VerticalGridPresenter.ViewHolder vh)

Called after a VerticalGridPresenter.ViewHolder is created.

public final booleanisFocusDimmerUsed()

Returns true if the focus dimmer is used for focus highlighting; false otherwise.

public booleanisUsingDefaultShadow()

Default implementation returns true if SDK version >= 21, shadow (either static or z-order based) will be applied to each individual child of VerticalGridView.

public booleanisUsingZOrder(Context context)

Returns true if SDK >= L, where Z shadow is enabled so that Z order is enabled on each child of vertical grid.

public abstract voidonBindViewHolder(Presenter.ViewHolder viewHolder, java.lang.Object item)

Binds a View to an item.

public abstract Presenter.ViewHolderonCreateViewHolder(ViewGroup parent)

Creates a new View.

public abstract voidonUnbindViewHolder(Presenter.ViewHolder viewHolder)

Unbinds a View from an item.

public voidsetEntranceTransitionState(VerticalGridPresenter.ViewHolder holder, boolean afterEntrance)

Changes the visibility of views.

public final voidsetKeepChildForeground(boolean keep)

Set if keeps foreground of child of this grid, the foreground will not be used for overlay color.

public voidsetNumberOfColumns(int numColumns)

Sets the number of columns in the vertical grid.

public final voidsetOnItemViewClickedListener(OnItemViewClickedListener listener)

Sets the item clicked listener.

public final voidsetOnItemViewSelectedListener(OnItemViewSelectedListener listener)

Sets the item selected listener.

public final voidsetShadowEnabled(boolean enabled)

Enable or disable child shadow.

from PresentercancelAnimationsRecursive, getFacet, onBindViewHolder, onViewAttachedToWindow, onViewDetachedFromWindow, setFacet, setOnClickListener
from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public VerticalGridPresenter()

Constructs a VerticalGridPresenter with defaults. Uses FocusHighlight.ZOOM_FACTOR_LARGE for focus zooming and enabled dimming on focus.

public VerticalGridPresenter(int focusZoomFactor)

Constructs a VerticalGridPresenter with the given parameters.

Parameters:

focusZoomFactor: Controls the zoom factor used when an item view is focused. One of FocusHighlight.ZOOM_FACTOR_NONE, FocusHighlight.ZOOM_FACTOR_SMALL, FocusHighlight.ZOOM_FACTOR_XSMALL, FocusHighlight.ZOOM_FACTOR_MEDIUM, FocusHighlight.ZOOM_FACTOR_LARGE enabled dimming on focus.

public VerticalGridPresenter(int focusZoomFactor, boolean useFocusDimmer)

Constructs a VerticalGridPresenter with the given parameters.

Parameters:

focusZoomFactor: Controls the zoom factor used when an item view is focused. One of FocusHighlight.ZOOM_FACTOR_NONE, FocusHighlight.ZOOM_FACTOR_SMALL, FocusHighlight.ZOOM_FACTOR_XSMALL, FocusHighlight.ZOOM_FACTOR_MEDIUM, FocusHighlight.ZOOM_FACTOR_LARGE
useFocusDimmer: determines if the FocusHighlighter will use the dimmer

Methods

public void setNumberOfColumns(int numColumns)

Sets the number of columns in the vertical grid.

public int getNumberOfColumns()

Returns the number of columns in the vertical grid.

public final void setShadowEnabled(boolean enabled)

Enable or disable child shadow. This is not only for enable/disable default shadow implementation but also subclass must respect this flag.

public final boolean getShadowEnabled()

Returns true if child shadow is enabled. This is not only for enable/disable default shadow implementation but also subclass must respect this flag.

public boolean isUsingDefaultShadow()

Default implementation returns true if SDK version >= 21, shadow (either static or z-order based) will be applied to each individual child of VerticalGridView. Subclass may return false to disable default implementation of shadow and provide its own.

public final void enableChildRoundedCorners(boolean enable)

Enables or disabled rounded corners on children of this row. Supported on Android SDK >= L.

public final boolean areChildRoundedCornersEnabled()

Returns true if rounded corners are enabled for children of this row.

public boolean isUsingZOrder(Context context)

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

public final int getFocusZoomFactor()

Returns the zoom factor used for focus highlighting.

public final boolean isFocusDimmerUsed()

Returns true if the focus dimmer is used for focus highlighting; false otherwise.

public abstract Presenter.ViewHolder onCreateViewHolder(ViewGroup parent)

Creates a new View.

protected VerticalGridPresenter.ViewHolder createGridViewHolder(ViewGroup parent)

Subclass may override this to inflate a different layout.

protected void initializeGridViewHolder(VerticalGridPresenter.ViewHolder vh)

Called after a VerticalGridPresenter.ViewHolder is created. Subclasses may override this method and start by calling super.initializeGridViewHolder(ViewHolder).

Parameters:

vh: The ViewHolder to initialize for the vertical grid.

public final void setKeepChildForeground(boolean keep)

Set if keeps foreground of child of this grid, the foreground will not be used for overlay color. Default value is true.

Parameters:

keep: True if keep foreground of child of this grid.

public final boolean getKeepChildForeground()

Returns true if keeps foreground of child of this grid, the foreground will not be used for overlay color. Default value is true.

Returns:

True if keeps foreground of child of this grid.

protected ShadowOverlayHelper.Options createShadowOverlayOptions()

Create ShadowOverlayHelper Options. Subclass may override. e.g. return new ShadowOverlayHelper.Options().roundedCornerRadius(10);

Returns:

The options to be used for shadow, overlay and rounded corner.

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

Binds a View to an item.

public abstract void onUnbindViewHolder(Presenter.ViewHolder viewHolder)

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.

public final void setOnItemViewSelectedListener(OnItemViewSelectedListener listener)

Sets the item selected listener. Since this is a grid the row parameter is always null.

public final OnItemViewSelectedListener getOnItemViewSelectedListener()

Returns the item selected listener.

public final void setOnItemViewClickedListener(OnItemViewClickedListener listener)

Sets the item clicked listener. OnItemViewClickedListener will override that item presenter sets during Presenter.onCreateViewHolder(ViewGroup). So in general, developer should choose one of the listeners but not both.

public final OnItemViewClickedListener getOnItemViewClickedListener()

Returns the item clicked listener.

public void setEntranceTransitionState(VerticalGridPresenter.ViewHolder holder, boolean afterEntrance)

Changes the visibility of views. The entrance transition will be run against the views that change visibilities. This method is called by the fragment, it should not be called directly by the application.

Parameters:

holder: The ViewHolder for the vertical grid.
afterEntrance: true if children of vertical grid participating in entrance transition should be set to visible, false otherwise.

Source

/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package androidx.leanback.widget;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.leanback.R;
import androidx.leanback.system.Settings;
import androidx.leanback.transition.TransitionHelper;

/**
 * A presenter that renders objects in a {@link VerticalGridView}.
 */
public class VerticalGridPresenter extends Presenter {
    private static final String TAG = "GridPresenter";
    private static final boolean DEBUG = false;

    class VerticalGridItemBridgeAdapter extends ItemBridgeAdapter {
        @Override
        protected void onCreate(ItemBridgeAdapter.ViewHolder viewHolder) {
            if (viewHolder.itemView instanceof ViewGroup) {
                TransitionHelper.setTransitionGroup((ViewGroup) viewHolder.itemView,
                        true);
            }
            if (mShadowOverlayHelper != null) {
                mShadowOverlayHelper.onViewCreated(viewHolder.itemView);
            }
        }

        @Override
        public void onBind(final ItemBridgeAdapter.ViewHolder itemViewHolder) {
            // Only when having an OnItemClickListener, we attach the OnClickListener.
            if (getOnItemViewClickedListener() != null) {
                final View itemView = itemViewHolder.mHolder.view;
                itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (getOnItemViewClickedListener() != null) {
                            // Row is always null
                            getOnItemViewClickedListener().onItemClicked(
                                    itemViewHolder.mHolder, itemViewHolder.mItem, null, null);
                        }
                    }
                });
            }
        }

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

        @Override
        public void onAttachedToWindow(ItemBridgeAdapter.ViewHolder viewHolder) {
            viewHolder.itemView.setActivated(true);
        }
    }

    /**
     * ViewHolder for the VerticalGridPresenter.
     */
    public static class ViewHolder extends Presenter.ViewHolder {
        ItemBridgeAdapter mItemBridgeAdapter;
        final VerticalGridView mGridView;
        boolean mInitialized;

        public ViewHolder(@NonNull VerticalGridView view) {
            super(view);
            mGridView = view;
        }

        @NonNull
        public VerticalGridView getGridView() {
            return mGridView;
        }
    }

    private int mNumColumns = -1;
    private int mFocusZoomFactor;
    private boolean mUseFocusDimmer;
    private boolean mShadowEnabled = true;
    private boolean mKeepChildForeground = true;
    private OnItemViewSelectedListener mOnItemViewSelectedListener;
    private OnItemViewClickedListener mOnItemViewClickedListener;
    private boolean mRoundedCornersEnabled = true;
    ShadowOverlayHelper mShadowOverlayHelper;
    private ItemBridgeAdapter.Wrapper mShadowOverlayWrapper;

    /**
     * Constructs a VerticalGridPresenter with defaults.
     * Uses {@link FocusHighlight#ZOOM_FACTOR_LARGE} for focus zooming and
     * enabled dimming on focus.
     */
    public VerticalGridPresenter() {
        this(FocusHighlight.ZOOM_FACTOR_LARGE);
    }

    /**
     * Constructs a VerticalGridPresenter with the given parameters.
     *
     * @param focusZoomFactor 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_XSMALL},
     *         {@link FocusHighlight#ZOOM_FACTOR_MEDIUM},
     *         {@link FocusHighlight#ZOOM_FACTOR_LARGE}
     * enabled dimming on focus.
     */
    public VerticalGridPresenter(int focusZoomFactor) {
        this(focusZoomFactor, true);
    }

    /**
     * Constructs a VerticalGridPresenter with the given parameters.
     *
     * @param focusZoomFactor 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_XSMALL},
     *         {@link FocusHighlight#ZOOM_FACTOR_MEDIUM},
     *         {@link FocusHighlight#ZOOM_FACTOR_LARGE}
     * @param useFocusDimmer determines if the FocusHighlighter will use the dimmer
     */
    public VerticalGridPresenter(int focusZoomFactor, boolean useFocusDimmer) {
        mFocusZoomFactor = focusZoomFactor;
        mUseFocusDimmer = useFocusDimmer;
    }

    /**
     * Sets the number of columns in the vertical grid.
     */
    public void setNumberOfColumns(int numColumns) {
        if (numColumns < 0) {
            throw new IllegalArgumentException("Invalid number of columns");
        }
        if (mNumColumns != numColumns) {
            mNumColumns = numColumns;
        }
    }

    /**
     * Returns the number of columns in the vertical grid.
     */
    public int getNumberOfColumns() {
        return mNumColumns;
    }

    /**
     * Enable or disable child shadow.
     * This is not only for enable/disable default shadow implementation but also subclass must
     * respect this flag.
     */
    public final void setShadowEnabled(boolean enabled) {
        mShadowEnabled = enabled;
    }

    /**
     * Returns true if child shadow is enabled.
     * This is not only for enable/disable default shadow implementation but also subclass must
     * respect this flag.
     */
    public final boolean getShadowEnabled() {
        return mShadowEnabled;
    }

    /**
     * Default implementation returns true if SDK version >= 21, shadow (either static or z-order
     * based) will be applied to each individual child of {@link VerticalGridView}.
     * Subclass may return false to disable default implementation of shadow and provide its own.
     */
    public boolean isUsingDefaultShadow() {
        return ShadowOverlayHelper.supportsShadow();
    }

    /**
     * Enables or disabled rounded corners on children of this row.
     * Supported on Android SDK >= L.
     */
    public final void enableChildRoundedCorners(boolean enable) {
        mRoundedCornersEnabled = enable;
    }

    /**
     * Returns true if rounded corners are enabled for children of this row.
     */
    public final boolean areChildRoundedCornersEnabled() {
        return mRoundedCornersEnabled;
    }

    /**
     * Returns true if SDK >= L, where Z shadow is enabled so that Z order is enabled
     * on each child of vertical grid.   If subclass returns false in isUsingDefaultShadow()
     * and does not use Z-shadow on SDK >= L, it should override isUsingZOrder() return false.
     */
    public boolean isUsingZOrder(@NonNull Context context) {
        return !Settings.getInstance(context).preferStaticShadows();
    }

    final boolean needsDefaultShadow() {
        return isUsingDefaultShadow() && getShadowEnabled();
    }

    /**
     * Returns the zoom factor used for focus highlighting.
     */
    public final int getFocusZoomFactor() {
        return mFocusZoomFactor;
    }

    /**
     * Returns true if the focus dimmer is used for focus highlighting; false otherwise.
     */
    public final boolean isFocusDimmerUsed() {
        return mUseFocusDimmer;
    }

    @NonNull
    @Override
    public final ViewHolder onCreateViewHolder(@NonNull ViewGroup parent) {
        ViewHolder vh = createGridViewHolder(parent);
        vh.mInitialized = false;
        vh.mItemBridgeAdapter = new VerticalGridItemBridgeAdapter();
        initializeGridViewHolder(vh);
        if (!vh.mInitialized) {
            throw new RuntimeException("super.initializeGridViewHolder() must be called");
        }
        return vh;
    }

    /**
     * Subclass may override this to inflate a different layout.
     */
    @NonNull
    protected ViewHolder createGridViewHolder(@NonNull ViewGroup parent) {
        View root = LayoutInflater.from(parent.getContext()).inflate(
                R.layout.lb_vertical_grid, parent, false);
        return new ViewHolder((VerticalGridView) root.findViewById(R.id.browse_grid));
    }

    /**
     * Called after a {@link VerticalGridPresenter.ViewHolder} is created.
     * Subclasses may override this method and start by calling
     * super.initializeGridViewHolder(ViewHolder).
     *
     * @param vh The ViewHolder to initialize for the vertical grid.
     */
    protected void initializeGridViewHolder(@NonNull ViewHolder vh) {
        if (mNumColumns == -1) {
            throw new IllegalStateException("Number of columns must be set");
        }
        if (DEBUG) Log.v(TAG, "mNumColumns " + mNumColumns);
        vh.getGridView().setNumColumns(mNumColumns);
        vh.mInitialized = true;

        Context context = vh.mGridView.getContext();
        if (mShadowOverlayHelper == null) {
            mShadowOverlayHelper = new ShadowOverlayHelper.Builder()
                    .needsOverlay(mUseFocusDimmer)
                    .needsShadow(needsDefaultShadow())
                    .needsRoundedCorner(areChildRoundedCornersEnabled())
                    .preferZOrder(isUsingZOrder(context))
                    .keepForegroundDrawable(mKeepChildForeground)
                    .options(createShadowOverlayOptions())
                    .build(context);
            if (mShadowOverlayHelper.needsWrapper()) {
                mShadowOverlayWrapper = new ItemBridgeAdapterShadowOverlayWrapper(
                        mShadowOverlayHelper);
            }
        }
        vh.mItemBridgeAdapter.setWrapper(mShadowOverlayWrapper);
        mShadowOverlayHelper.prepareParentForShadow(vh.mGridView);
        vh.getGridView().setFocusDrawingOrderEnabled(mShadowOverlayHelper.getShadowType()
                != ShadowOverlayHelper.SHADOW_DYNAMIC);
        FocusHighlightHelper.setupBrowseItemFocusHighlight(vh.mItemBridgeAdapter,
                mFocusZoomFactor, mUseFocusDimmer);

        final ViewHolder gridViewHolder = vh;
        vh.getGridView().setOnChildSelectedListener(new OnChildSelectedListener() {
            @Override
            public void onChildSelected(
                    @NonNull ViewGroup parent,
                    @Nullable View view,
                    int position,
                    long id
            ) {
                selectChildView(gridViewHolder, view);
            }
        });
    }

    /**
     * Set if keeps foreground of child of this grid, the foreground will not
     * be used for overlay color.  Default value is true.
     *
     * @param keep   True if keep foreground of child of this grid.
     */
    public final void setKeepChildForeground(boolean keep) {
        mKeepChildForeground = keep;
    }

    /**
     * Returns true if keeps foreground of child of this grid, the foreground will not
     * be used for overlay color.  Default value is true.
     *
     * @return   True if keeps foreground of child of this grid.
     */
    public final boolean getKeepChildForeground() {
        return mKeepChildForeground;
    }

    /**
     * Create ShadowOverlayHelper Options.  Subclass may override.
     * e.g.
     * <code>
     * return new ShadowOverlayHelper.Options().roundedCornerRadius(10);
     * </code>
     *
     * @return   The options to be used for shadow, overlay and rounded corner.
     */
    @NonNull
    protected ShadowOverlayHelper.Options createShadowOverlayOptions() {
        return ShadowOverlayHelper.Options.DEFAULT;
    }

    @Override
    public void onBindViewHolder(@NonNull Presenter.ViewHolder viewHolder, @Nullable Object item) {
        if (DEBUG) Log.v(TAG, "onBindViewHolder " + item);
        ViewHolder vh = (ViewHolder) viewHolder;
        vh.mItemBridgeAdapter.setAdapter((ObjectAdapter) item);
        vh.getGridView().setAdapter(vh.mItemBridgeAdapter);
    }

    @Override
    public void onUnbindViewHolder(@NonNull Presenter.ViewHolder viewHolder) {
        if (DEBUG) Log.v(TAG, "onUnbindViewHolder");
        ViewHolder vh = (ViewHolder) viewHolder;
        vh.mItemBridgeAdapter.setAdapter(null);
        vh.getGridView().setAdapter(null);
    }

    /**
     * Sets the item selected listener.
     * Since this is a grid the row parameter is always null.
     */
    public final void setOnItemViewSelectedListener(@Nullable OnItemViewSelectedListener listener) {
        mOnItemViewSelectedListener = listener;
    }

    /**
     * Returns the item selected listener.
     */
    @Nullable
    public final OnItemViewSelectedListener getOnItemViewSelectedListener() {
        return mOnItemViewSelectedListener;
    }

    /**
     * Sets the item clicked listener.
     * 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.
     */
    public final void setOnItemViewClickedListener(@Nullable OnItemViewClickedListener listener) {
        mOnItemViewClickedListener = listener;
    }

    /**
     * Returns the item clicked listener.
     */
    @Nullable
    public final OnItemViewClickedListener getOnItemViewClickedListener() {
        return mOnItemViewClickedListener;
    }

    void selectChildView(ViewHolder vh, View view) {
        if (getOnItemViewSelectedListener() != null) {
            ItemBridgeAdapter.ViewHolder ibh = (view == null) ? null :
                    (ItemBridgeAdapter.ViewHolder) vh.getGridView().getChildViewHolder(view);
            if (ibh == null) {
                getOnItemViewSelectedListener().onItemSelected(null, null, null, null);
            } else {
                getOnItemViewSelectedListener().onItemSelected(ibh.mHolder, ibh.mItem, null, null);
            }
        }
    }

    /**
     * Changes the visibility of views.  The entrance transition will be run against the views that
     * change visibilities.  This method is called by the fragment, it should not be called
     * directly by the application.
     *
     * @param holder         The ViewHolder for the vertical grid.
     * @param afterEntrance  true if children of vertical grid participating in entrance transition
     *                       should be set to visible, false otherwise.
     */
    public void setEntranceTransitionState(@NonNull ViewHolder holder,
            boolean afterEntrance) {
        holder.mGridView.setChildrenVisibility(afterEntrance? View.VISIBLE : View.INVISIBLE);
    }
}