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

BaseGridView

public abstract class BaseGridView extends android.support.v7.widget.RecyclerView
Base class for vertically and horizontally scrolling lists. The items come from the {@link RecyclerView.Adapter} associated with this view.
hide

Fields Summary
public static final int
FOCUS_SCROLL_ALIGNED
Always keep focused item at a aligned position. Developer can use WINDOW_ALIGN_XXX and ITEM_ALIGN_XXX to define how focused item is aligned. In this mode, the last focused position will be remembered and restored when focus is back to the view.
public static final int
FOCUS_SCROLL_ITEM
Scroll to make the focused item inside client area.
public static final int
FOCUS_SCROLL_PAGE
Scroll a page of items when focusing to item outside the client area. The page size matches the client area size of RecyclerView.
public static final int
WINDOW_ALIGN_LOW_EDGE
The first item is aligned with the low edge of the viewport. When navigating away from the first item, the focus maintains a middle location.

For HorizontalGridView, low edge refers to left edge when RTL is false or right edge when RTL is true. For VerticalGridView, low edge refers to top edge.

The middle location is calculated by "windowAlignOffset" and "windowAlignOffsetPercent"; if neither of these two is defined, the default value is 1/2 of the size.

public static final int
WINDOW_ALIGN_HIGH_EDGE
The last item is aligned with the high edge of the viewport when navigating to the end of list. When navigating away from the end, the focus maintains a middle location.

For HorizontalGridView, high edge refers to right edge when RTL is false or left edge when RTL is true. For VerticalGridView, high edge refers to bottom edge.

The middle location is calculated by "windowAlignOffset" and "windowAlignOffsetPercent"; if neither of these two is defined, the default value is 1/2 of the size.

public static final int
WINDOW_ALIGN_BOTH_EDGE
The first item and last item are aligned with the two edges of the viewport. When navigating in the middle of list, the focus maintains a middle location.

The middle location is calculated by "windowAlignOffset" and "windowAlignOffsetPercent"; if neither of these two is defined, the default value is 1/2 of the size.

public static final int
WINDOW_ALIGN_NO_EDGE
The focused item always stays in a middle location.

The middle location is calculated by "windowAlignOffset" and "windowAlignOffsetPercent"; if neither of these two is defined, the default value is 1/2 of the size.

public static final float
WINDOW_ALIGN_OFFSET_PERCENT_DISABLED
Value indicates that percent is not used.
public static final float
ITEM_ALIGN_OFFSET_PERCENT_DISABLED
Value indicates that percent is not used.
public static final int
SAVE_NO_CHILD
Dont save states of any child views.
public static final int
SAVE_ON_SCREEN_CHILD
Only save on screen child views, the states are lost when they become off screen.
public static final int
SAVE_LIMITED_CHILD
Save on screen views plus save off screen child views states up to {@link #getSaveChildrenLimitNumber()}.
public static final int
SAVE_ALL_CHILD
Save on screen views plus save off screen child views without any limitation. This might cause out of memory, only use it when you are dealing with limited data.
protected final GridLayoutManager
mLayoutManager
private boolean
mAnimateChildLayout
Animate layout changes from a child resizing or adding/removing a child.
private boolean
mHasOverlappingRendering
private RecyclerView.ItemAnimator
mSavedItemAnimator
private OnTouchInterceptListener
mOnTouchInterceptListener
private OnMotionInterceptListener
mOnMotionInterceptListener
private OnKeyInterceptListener
mOnKeyInterceptListener
Constructors Summary
public BaseGridView(android.content.Context context, android.util.AttributeSet attrs, int defStyle)


           
        super(context, attrs, defStyle);
        mLayoutManager = new GridLayoutManager(this);
        setLayoutManager(mLayoutManager);
        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
        setHasFixedSize(true);
        setChildrenDrawingOrderEnabled(true);
        setWillNotDraw(true);
        setOverScrollMode(View.OVER_SCROLL_NEVER);
        // Disable change animation by default on leanback.
        // Change animation will create a new view and cause undesired
        // focus animation between the old view and new view.
        getItemAnimator().setSupportsChangeAnimations(false);
    
Methods Summary
public booleandispatchGenericFocusedEvent(android.view.MotionEvent event)

        if (mOnMotionInterceptListener != null) {
            if (mOnMotionInterceptListener.onInterceptMotionEvent(event)) {
                return true;
            }
        }
        return super.dispatchGenericFocusedEvent(event);
    
public booleandispatchKeyEvent(android.view.KeyEvent event)

        if (mOnKeyInterceptListener != null) {
            if (mOnKeyInterceptListener.onInterceptKeyEvent(event)) {
                return true;
            }
        }
        return super.dispatchKeyEvent(event);
    
public booleandispatchTouchEvent(android.view.MotionEvent event)

        if (mOnTouchInterceptListener != null) {
            if (mOnTouchInterceptListener.onInterceptTouchEvent(event)) {
                return true;
            }
        }
        return super.dispatchTouchEvent(event);
    
public intgetChildDrawingOrder(int childCount, int i)

        return mLayoutManager.getChildDrawingOrder(this, childCount, i);
    
public intgetFocusScrollStrategy()
Returns the strategy used to scroll in response to item focus changing.
  • {@link #FOCUS_SCROLL_ALIGNED} (default)
  • {@link #FOCUS_SCROLL_ITEM}
  • {@link #FOCUS_SCROLL_PAGE}

        return mLayoutManager.getFocusScrollStrategy();
    
public intgetHorizontalMargin()
Get the margin in pixels between two child items horizontally.

        return mLayoutManager.getHorizontalMargin();
    
public intgetItemAlignmentOffset()
Get the absolute offset in pixels for item alignment.

return
The number of pixels to offset. Will be negative for alignment from the high edge, or positive for alignment from the low edge. Default value is 0.

        return mLayoutManager.getItemAlignmentOffset();
    
public floatgetItemAlignmentOffsetPercent()
Get offset percent for item alignment in addition to {@link #getItemAlignmentOffset()}.

return
Percentage to offset. E.g., 40 means 40% of the width from the low edge, or {@link #ITEM_ALIGN_OFFSET_PERCENT_DISABLED} if disabled. Default value is 50.

        return mLayoutManager.getItemAlignmentOffsetPercent();
    
public intgetItemAlignmentViewId()
Get the id of the view to align with, or zero for the item view itself.

        return mLayoutManager.getItemAlignmentViewId();
    
public final intgetSaveChildrenLimitNumber()

return
The limit number when {@link #getSaveChildrenPolicy()} is {@link #SAVE_LIMITED_CHILD}

        return mLayoutManager.mChildrenStates.getLimitNumber();
    
public final intgetSaveChildrenPolicy()

return
policy for saving children. One of {@link #SAVE_NO_CHILD} {@link #SAVE_ON_SCREEN_CHILD} {@link #SAVE_LIMITED_CHILD} {@link #SAVE_ALL_CHILD}.

        return mLayoutManager.mChildrenStates.getSavePolicy();
    
public intgetSelectedPosition()
Get the selected item position.

        return mLayoutManager.getSelection();
    
public intgetVerticalMargin()
Get the margin in pixels between two child items vertically.

        return mLayoutManager.getVerticalMargin();
    
public voidgetViewSelectedOffsets(android.view.View view, int[] offsets)
Get the x/y offsets to final position from current position if the view is selected.

param
view The view to get offsets.
param
offsets offsets[0] holds offset of X, offsets[1] holds offset of Y.

        mLayoutManager.getViewSelectedOffsets(view, offsets);
    
public intgetWindowAlignment()
Get how the focused item gets aligned in the view.

return
{@link #WINDOW_ALIGN_BOTH_EDGE}, {@link #WINDOW_ALIGN_LOW_EDGE}, {@link #WINDOW_ALIGN_HIGH_EDGE} or {@link #WINDOW_ALIGN_NO_EDGE}.

        return mLayoutManager.getWindowAlignment();
    
public intgetWindowAlignmentOffset()
Get the offset in pixels for window alignment.

return
The number of pixels to offset. If the offset is positive, it is distance from low edge (see {@link #WINDOW_ALIGN_LOW_EDGE}); if the offset is negative, the absolute value is distance from high edge (see {@link #WINDOW_ALIGN_HIGH_EDGE}). Default value is 0.

        return mLayoutManager.getWindowAlignmentOffset();
    
public floatgetWindowAlignmentOffsetPercent()
Get offset percent for window alignment in addition to {@link #getWindowAlignmentOffset()}.

return
Percentage to offset. E.g., 40 means 40% of the width from the low edge, or {@link #WINDOW_ALIGN_OFFSET_PERCENT_DISABLED} if disabled. Default value is 50.

        return mLayoutManager.getWindowAlignmentOffsetPercent();
    
public booleanhasOverlappingRendering()

        return mHasOverlappingRendering;
    
public booleanhasPreviousViewInSameRow(int position)
Returns true if the view at the given position has a same row sibling in front of it.

param
position Position in adapter.

        return mLayoutManager.hasPreviousViewInSameRow(position);
    
protected voidinitBaseGridViewAttributes(android.content.Context context, android.util.AttributeSet attrs)

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.lbBaseGridView);
        boolean throughFront = a.getBoolean(R.styleable.lbBaseGridView_focusOutFront, false);
        boolean throughEnd = a.getBoolean(R.styleable.lbBaseGridView_focusOutEnd, false);
        mLayoutManager.setFocusOutAllowed(throughFront, throughEnd);
        mLayoutManager.setVerticalMargin(
                a.getDimensionPixelSize(R.styleable.lbBaseGridView_verticalMargin, 0));
        mLayoutManager.setHorizontalMargin(
                a.getDimensionPixelSize(R.styleable.lbBaseGridView_horizontalMargin, 0));
        if (a.hasValue(R.styleable.lbBaseGridView_android_gravity)) {
            setGravity(a.getInt(R.styleable.lbBaseGridView_android_gravity, Gravity.NO_GRAVITY));
        }
        a.recycle();
    
public booleanisChildLayoutAnimated()
Return true if an animation will run when a child changes size or when adding or removing a child.

Unstable API, might change later.

        return mAnimateChildLayout;
    
final booleanisChildrenDrawingOrderEnabledInternal()

        return isChildrenDrawingOrderEnabled();
    
public booleanisFocusDrawingOrderEnabled()
Returns true if default "focus draw at last" order rule is enabled.

        return super.isChildrenDrawingOrderEnabled();
    
public final booleanisFocusSearchDisabled()
Return true if focus search is disabled.

        return mLayoutManager.isFocusSearchDisabled();
    
public booleanisItemAlignmentOffsetWithPadding()
Returns true if include padding in calculating item align offset.

        return mLayoutManager.isItemAlignmentOffsetWithPadding();
    
public booleanisScrollEnabled()
Returns true if scrolling is enabled.

        return mLayoutManager.isScrollEnabled();
    
public booleanonRequestFocusInDescendants(int direction, android.graphics.Rect previouslyFocusedRect)

        return mLayoutManager.gridOnRequestFocusInDescendants(this, direction,
                previouslyFocusedRect);
    
public voidonRtlPropertiesChanged(int layoutDirection)
Notify layout manager that layout directionality has been updated

        mLayoutManager.onRtlPropertiesChanged(layoutDirection);
    
public voidsetAnimateChildLayout(boolean animateChildLayout)
Set if an animation should run when a child changes size or when adding or removing a child.

Unstable API, might change later.

        if (mAnimateChildLayout != animateChildLayout) {
            mAnimateChildLayout = animateChildLayout;
            if (!mAnimateChildLayout) {
                mSavedItemAnimator = getItemAnimator();
                super.setItemAnimator(null);
            } else {
                super.setItemAnimator(mSavedItemAnimator);
            }
        }
    
public voidsetChildrenVisibility(int visibility)
Change and override children's visibility.

        mLayoutManager.setChildrenVisibility(visibility);
    
public voidsetFocusDrawingOrderEnabled(boolean enabled)
Enable or disable the default "focus draw at last" order rule.

        super.setChildrenDrawingOrderEnabled(enabled);
    
public voidsetFocusScrollStrategy(int scrollStrategy)
Set the strategy used to scroll in response to item focus changing:
  • {@link #FOCUS_SCROLL_ALIGNED} (default)
  • {@link #FOCUS_SCROLL_ITEM}
  • {@link #FOCUS_SCROLL_PAGE}

        if (scrollStrategy != FOCUS_SCROLL_ALIGNED && scrollStrategy != FOCUS_SCROLL_ITEM
            && scrollStrategy != FOCUS_SCROLL_PAGE) {
            throw new IllegalArgumentException("Invalid scrollStrategy");
        }
        mLayoutManager.setFocusScrollStrategy(scrollStrategy);
        requestLayout();
    
public final voidsetFocusSearchDisabled(boolean disabled)
Disable or enable focus search.

        mLayoutManager.setFocusSearchDisabled(disabled);
    
public voidsetGravity(int gravity)
Describes how the child views are positioned. Defaults to GRAVITY_TOP|GRAVITY_START.

param
gravity See {@link android.view.Gravity}

        mLayoutManager.setGravity(gravity);
        requestLayout();
    
public voidsetHasOverlappingRendering(boolean hasOverlapping)

        mHasOverlappingRendering = hasOverlapping;
    
public voidsetHorizontalMargin(int margin)
Set the margin in pixels between two child items horizontally.

        mLayoutManager.setHorizontalMargin(margin);
        requestLayout();
    
public voidsetItemAlignmentOffset(int offset)
Set the absolute offset in pixels for item alignment.

param
offset The number of pixels to offset. Can be negative for alignment from the high edge, or positive for alignment from the low edge.

        mLayoutManager.setItemAlignmentOffset(offset);
        requestLayout();
    
public voidsetItemAlignmentOffsetPercent(float offsetPercent)
Set offset percent for item alignment in addition to {@link #getItemAlignmentOffset()}.

param
offsetPercent Percentage to offset. E.g., 40 means 40% of the width from the low edge. Use {@link #ITEM_ALIGN_OFFSET_PERCENT_DISABLED} to disable.

        mLayoutManager.setItemAlignmentOffsetPercent(offsetPercent);
        requestLayout();
    
public voidsetItemAlignmentOffsetWithPadding(boolean withPadding)
Set to true if include padding in calculating item align offset.

param
withPadding When it is true: we include left/top padding for positive item offset, include right/bottom padding for negative item offset.

        mLayoutManager.setItemAlignmentOffsetWithPadding(withPadding);
        requestLayout();
    
public voidsetItemAlignmentViewId(int viewId)
Set the id of the view to align with. Use zero (default) for the item view itself.

        mLayoutManager.setItemAlignmentViewId(viewId);
    
public voidsetItemMargin(int margin)
Set the margin in pixels between two child items.

        mLayoutManager.setItemMargin(margin);
        requestLayout();
    
public voidsetLayoutEnabled(boolean layoutEnabled)
Enable or disable layout. All children will be removed when layout is disabled.

        mLayoutManager.setLayoutEnabled(layoutEnabled);
    
public voidsetOnChildSelectedListener(OnChildSelectedListener listener)
Register a callback to be invoked when an item in BaseGridView has been selected. Note that the listener may be invoked when there is a layout pending on the view, affording the listener an opportunity to adjust the upcoming layout based on the selection state.

param
listener The listener to be invoked.

        mLayoutManager.setOnChildSelectedListener(listener);
    
public voidsetOnKeyInterceptListener(android.support.v17.leanback.widget.BaseGridView$OnKeyInterceptListener listener)
Sets the key intercept listener.

        mOnKeyInterceptListener = listener;
    
public voidsetOnMotionInterceptListener(android.support.v17.leanback.widget.BaseGridView$OnMotionInterceptListener listener)
Sets the generic motion intercept listener.

        mOnMotionInterceptListener = listener;
    
public voidsetOnTouchInterceptListener(android.support.v17.leanback.widget.BaseGridView$OnTouchInterceptListener listener)
Sets the touch intercept listener.

        mOnTouchInterceptListener = listener;
    
public voidsetPruneChild(boolean pruneChild)
Enable or disable pruning child. Disable is useful during transition.

        mLayoutManager.setPruneChild(pruneChild);
    
public final voidsetSaveChildrenLimitNumber(int limitNumber)
Set limit number when {@link #getSaveChildrenPolicy()} is {@link #SAVE_LIMITED_CHILD}.

        mLayoutManager.mChildrenStates.setLimitNumber(limitNumber);
    
public final voidsetSaveChildrenPolicy(int savePolicy)
Set policy for saving children.

param
savePolicy One of {@link #SAVE_NO_CHILD} {@link #SAVE_ON_SCREEN_CHILD} {@link #SAVE_LIMITED_CHILD} {@link #SAVE_ALL_CHILD}.

        mLayoutManager.mChildrenStates.setSavePolicy(savePolicy);
    
public voidsetScrollEnabled(boolean scrollEnabled)
Enable or disable scrolling. Disable is useful during transition.

        mLayoutManager.setScrollEnabled(scrollEnabled);
    
public voidsetSelectedPosition(int position)
Change the selected item immediately without animation.

        mLayoutManager.setSelection(this, position);
    
public voidsetSelectedPositionSmooth(int position)
Change the selected item and run an animation to scroll to the target position.

        mLayoutManager.setSelectionSmooth(this, position);
    
public voidsetVerticalMargin(int margin)
Set the margin in pixels between two child items vertically.

        mLayoutManager.setVerticalMargin(margin);
        requestLayout();
    
public voidsetWindowAlignment(int windowAlignment)
Set how the focused item gets aligned in the view.

param
windowAlignment {@link #WINDOW_ALIGN_BOTH_EDGE}, {@link #WINDOW_ALIGN_LOW_EDGE}, {@link #WINDOW_ALIGN_HIGH_EDGE} or {@link #WINDOW_ALIGN_NO_EDGE}.

        mLayoutManager.setWindowAlignment(windowAlignment);
        requestLayout();
    
public voidsetWindowAlignmentOffset(int offset)
Set the offset in pixels for window alignment.

param
offset The number of pixels to offset. If the offset is positive, it is distance from low edge (see {@link #WINDOW_ALIGN_LOW_EDGE}); if the offset is negative, the absolute value is distance from high edge (see {@link #WINDOW_ALIGN_HIGH_EDGE}). Default value is 0.

        mLayoutManager.setWindowAlignmentOffset(offset);
        requestLayout();
    
public voidsetWindowAlignmentOffsetPercent(float offsetPercent)
Set offset percent for window alignment in addition to {@link #getWindowAlignmentOffset()}.

param
offsetPercent Percentage to offset. E.g., 40 means 40% of the width from low edge. Use {@link #WINDOW_ALIGN_OFFSET_PERCENT_DISABLED} to disable. Default value is 50.

        mLayoutManager.setWindowAlignmentOffsetPercent(offsetPercent);
        requestLayout();