Android APIs
public class

HorizontalGridView

extends RecyclerView
java.lang.Object
   ↳ android.view.View
     ↳ android.view.ViewGroup
       ↳ android.support.v7.widget.RecyclerView
         ↳ android.support.v17.leanback.widget.HorizontalGridView

Class Overview

A view that shows items in a horizontal scrolling list. The items come from the RecyclerView.Adapter associated with this view.

Summary

[Expand]
Inherited XML Attributes
From class android.view.ViewGroup
From class android.view.View
Constants
int FOCUS_SCROLL_ALIGNED Always keep focused item at a aligned position.
int FOCUS_SCROLL_ITEM Scroll to make the focused item inside client area.
int FOCUS_SCROLL_PAGE Scroll a page of items when focusing to item outside the client area.
float ITEM_ALIGN_OFFSET_PERCENT_DISABLED Value indicates that percent is not used.
int WINDOW_ALIGN_BOTH_EDGE The first item and last item are aligned with the two edges of the viewport.
int WINDOW_ALIGN_HIGH_EDGE The last item is aligned with the high edge of the viewport when navigating to the end of list.
int WINDOW_ALIGN_LOW_EDGE The first item is aligned with the low edge of the viewport.
int WINDOW_ALIGN_NO_EDGE The focused item always stays in a middle location.
float WINDOW_ALIGN_OFFSET_PERCENT_DISABLED Value indicates that percent is not used.
[Expand]
Inherited Constants
From class android.support.v7.widget.RecyclerView
From class android.view.ViewGroup
From class android.view.View
Fields
protected final GridLayoutManager mLayoutManager
[Expand]
Inherited Fields
From class android.view.View
Public Constructors
HorizontalGridView(Context context)
HorizontalGridView(Context context, AttributeSet attrs)
HorizontalGridView(Context context, AttributeSet attrs, int defStyle)
Public Methods
void draw(Canvas canvas)
Manually render this view (and all of its children) to the given Canvas.
int getChildDrawingOrder(int childCount, int i)
Returns the index of the child to draw for this iteration.
final boolean getFadingLeftEdge()
Return true if fading left edge.
final int getFadingLeftEdgeLength()
Get left edge fading length in pixels.
final int getFadingLeftEdgeOffset()
Get distance in pixels between fading start position and left padding edge.
final boolean getFadingRightEdge()
Return true if fading right edge.
final int getFadingRightEdgeLength()
Get right edge fading length in pixels.
final int getFadingRightEdgeOffset()
Set distance in pixels between fading start position and right padding edge.
int getFocusScrollStrategy()
Returns the strategy used to scroll in response to item focus changing.
int getHorizontalMargin()
Get the margin in pixels between two child items horizontally.
int getItemAlignmentOffset()
Get the absolute offset in pixels for item alignment.
float getItemAlignmentOffsetPercent()
Get offset percent for item alignment in addition to getItemAlignmentOffset().
int getItemAlignmentViewId()
Get the id of the view to align with, or zero for the item view itself.
int getSelectedPosition()
Get the selected item position.
int getVerticalMargin()
Get the margin in pixels between two child items vertically.
void getViewSelectedOffsets(View view, int[] offsets)
Get the x/y offsets to final position from current position if the view is selected.
int getWindowAlignment()
Get how the focused item gets aligned in the view.
int getWindowAlignmentOffset()
Get the absolute offset in pixels for window alignment.
float getWindowAlignmentOffsetPercent()
Get offset percent for window alignment in addition to getWindowAlignmentOffset().
boolean hasPreviousViewInSameRow(int position)
Returns true if the view at the given position has a same row sibling in front of it.
boolean isChildLayoutAnimated()
Return true if an animation will run when a child changes size or when adding or removing a child.
final boolean isFocusSearchDisabled()
Return true if focus search is disabled.
boolean isItemAlignmentOffsetWithPadding()
Returns true if include padding in calculating item align offset.
boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
Look for a descendant to call requestFocus() on.
void setAnimateChildLayout(boolean animateChildLayout)
Set if an animation should run when a child changes size or when adding or removing a child.
void setDescendantFocusability(int focusability)
Set the descendant focusability of this view group.
final void setFadingLeftEdge(boolean fading)
Set fade out left edge to transparent.
final void setFadingLeftEdgeLength(int fadeLength)
Set left edge fading length in pixels.
final void setFadingLeftEdgeOffset(int fadeOffset)
Set distance in pixels between fading start position and left padding edge.
final void setFadingRightEdge(boolean fading)
Set fade out right edge to transparent.
final void setFadingRightEdgeLength(int fadeLength)
Set right edge fading length in pixels.
final void setFadingRightEdgeOffset(int fadeOffset)
Get distance in pixels between fading start position and right padding edge.
void setFocusScrollStrategy(int scrollStrategy)
Set the strategy used to scroll in response to item focus changing:
final void setFocusSearchDisabled(boolean disabled)
Disable or enable focus search.
void setGravity(int gravity)
Describes how the child views are positioned.
void setHorizontalMargin(int margin)
Set the margin in pixels between two child items horizontally.
void setItemAlignmentOffset(int offset)
Set the absolute offset in pixels for item alignment.
void setItemAlignmentOffsetPercent(float offsetPercent)
Set offset percent for item alignment in addition to getItemAlignmentOffset().
void setItemAlignmentOffsetWithPadding(boolean withPadding)
Set to true if include padding in calculating item align offset.
void setItemAlignmentViewId(int viewId)
Set the id of the view to align with.
void setItemMargin(int margin)
Set the margin in pixels between two child items.
void setLayoutEnabled(boolean layoutEnabled)
Enable or disable layout.
void setNumRows(int numRows)
Set the number of rows.
void setOnChildSelectedListener(OnChildSelectedListener listener)
Register a callback to be invoked when an item in BaseGridView has been selected.
void setPruneChild(boolean pruneChild)
Enable or disable pruning child.
void setRowHeight(int height)
Set the row height.
void setSelectedPosition(int position)
Change the selected item immediately without animation.
void setSelectedPositionSmooth(int position)
Change the selected item and run an animation to scroll to the target position.
void setVerticalMargin(int margin)
Set the margin in pixels between two child items vertically.
void setWindowAlignment(int windowAlignment)
Set how the focused item gets aligned in the view.
void setWindowAlignmentOffset(int offset)
Set the absolute offset in pixels for window alignment.
void setWindowAlignmentOffsetPercent(float offsetPercent)
Set offset percent for window alignment in addition to getWindowAlignmentOffset().
Protected Methods
void initAttributes(Context context, AttributeSet attrs)
void initBaseGridViewAttributes(Context context, AttributeSet attrs)
[Expand]
Inherited Methods
From class android.support.v7.widget.RecyclerView
From class android.view.ViewGroup
From class android.view.View
From class java.lang.Object
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
From interface android.view.ViewManager
From interface android.view.ViewParent
From interface android.view.accessibility.AccessibilityEventSource

Constants

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.

Constant Value: 0 (0x00000000)

public static final int FOCUS_SCROLL_ITEM

Scroll to make the focused item inside client area.

Constant Value: 1 (0x00000001)

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.

Constant Value: 2 (0x00000002)

public static final float ITEM_ALIGN_OFFSET_PERCENT_DISABLED

Value indicates that percent is not used.

Constant Value: -1.0

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.

Constant Value: 3 (0x00000003)

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.

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.

Constant Value: 2 (0x00000002)

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.

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.

Constant Value: 1 (0x00000001)

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.

Constant Value: 0 (0x00000000)

public static final float WINDOW_ALIGN_OFFSET_PERCENT_DISABLED

Value indicates that percent is not used.

Constant Value: -1.0

Fields

protected final GridLayoutManager mLayoutManager

Public Constructors

public HorizontalGridView (Context context)

public HorizontalGridView (Context context, AttributeSet attrs)

public HorizontalGridView (Context context, AttributeSet attrs, int defStyle)

Public Methods

public void draw (Canvas canvas)

Manually render this view (and all of its children) to the given Canvas. The view must have already done a full layout before this function is called. When implementing a view, implement onDraw(android.graphics.Canvas) instead of overriding this method. If you do need to override this method, call the superclass version.

Parameters
canvas The Canvas to which the View is rendered.

public int getChildDrawingOrder (int childCount, int i)

Returns the index of the child to draw for this iteration. Override this if you want to change the drawing order of children. By default, it returns i.

NOTE: In order for this method to be called, you must enable child ordering first by calling setChildrenDrawingOrderEnabled(boolean).

Parameters
i The current iteration.
Returns
  • The index of the child to draw this iteration.

public final boolean getFadingLeftEdge ()

Return true if fading left edge.

public final int getFadingLeftEdgeLength ()

Get left edge fading length in pixels.

public final int getFadingLeftEdgeOffset ()

Get distance in pixels between fading start position and left padding edge. The fading start position is positive when start position is inside left padding area. Default value is 0, means that the fading starts from left padding edge.

public final boolean getFadingRightEdge ()

Return true if fading right edge.

public final int getFadingRightEdgeLength ()

Get right edge fading length in pixels.

public final int getFadingRightEdgeOffset ()

Set distance in pixels between fading start position and right padding edge. The fading start position is positive when start position is inside right padding area. Default value is 0, means that the fading starts from right padding edge.

public int getFocusScrollStrategy ()

Returns the strategy used to scroll in response to item focus changing.

public int getHorizontalMargin ()

Get the margin in pixels between two child items horizontally.

public int getItemAlignmentOffset ()

Get the absolute offset in pixels for item alignment.

Returns
  • 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.

public float getItemAlignmentOffsetPercent ()

Get offset percent for item alignment in addition to getItemAlignmentOffset().

Returns

public int getItemAlignmentViewId ()

Get the id of the view to align with, or zero for the item view itself.

public int getSelectedPosition ()

Get the selected item position.

public int getVerticalMargin ()

Get the margin in pixels between two child items vertically.

public void getViewSelectedOffsets (View view, int[] offsets)

Get the x/y offsets to final position from current position if the view is selected.

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

public int getWindowAlignment ()

Get how the focused item gets aligned in the view.

public int getWindowAlignmentOffset ()

Get the absolute offset in pixels for window alignment.

Returns
  • 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.

public float getWindowAlignmentOffsetPercent ()

Get offset percent for window alignment in addition to getWindowAlignmentOffset().

Returns

public boolean hasPreviousViewInSameRow (int position)

Returns true if the view at the given position has a same row sibling in front of it.

Parameters
position Position in adapter.

public boolean isChildLayoutAnimated ()

Return true if an animation will run when a child changes size or when adding or removing a child.

Unstable API, might change later.

public final boolean isFocusSearchDisabled ()

Return true if focus search is disabled.

public boolean isItemAlignmentOffsetWithPadding ()

Returns true if include padding in calculating item align offset.

public boolean onRequestFocusInDescendants (int direction, Rect previouslyFocusedRect)

Look for a descendant to call requestFocus() on. Called by requestFocus(int, android.graphics.Rect) when it wants to request focus within its children. Override this to customize how your ViewGroup requests focus within its children.

Parameters
direction One of FOCUS_UP, FOCUS_DOWN, FOCUS_LEFT, and FOCUS_RIGHT
previouslyFocusedRect The rectangle (in this View's coordinate system) to give a finer grained hint about where focus is coming from. May be null if there is no hint.
Returns
  • Whether focus was taken.

public void setAnimateChildLayout (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.

public void setDescendantFocusability (int focusability)

Set the descendant focusability of this view group. This defines the relationship between this view group and its descendants when looking for a view to take focus in requestFocus(int, android.graphics.Rect).

public final void setFadingLeftEdge (boolean fading)

Set fade out left edge to transparent. Note turn on fading edge is very expensive that you should turn off when HorizontalGridView is scrolling.

public final void setFadingLeftEdgeLength (int fadeLength)

Set left edge fading length in pixels.

public final void setFadingLeftEdgeOffset (int fadeOffset)

Set distance in pixels between fading start position and left padding edge. The fading start position is positive when start position is inside left padding area. Default value is 0, means that the fading starts from left padding edge.

public final void setFadingRightEdge (boolean fading)

Set fade out right edge to transparent. Note turn on fading edge is very expensive that you should turn off when HorizontalGridView is scrolling.

public final void setFadingRightEdgeLength (int fadeLength)

Set right edge fading length in pixels.

public final void setFadingRightEdgeOffset (int fadeOffset)

Get distance in pixels between fading start position and right padding edge. The fading start position is positive when start position is inside right padding area. Default value is 0, means that the fading starts from right padding edge.

public void setFocusScrollStrategy (int scrollStrategy)

Set the strategy used to scroll in response to item focus changing:

public final void setFocusSearchDisabled (boolean disabled)

Disable or enable focus search.

public void setGravity (int gravity)

Describes how the child views are positioned. Defaults to GRAVITY_TOP|GRAVITY_LEFT.

Parameters
gravity See Gravity

public void setHorizontalMargin (int margin)

Set the margin in pixels between two child items horizontally.

public void setItemAlignmentOffset (int offset)

Set the absolute offset in pixels for item alignment.

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

public void setItemAlignmentOffsetPercent (float offsetPercent)

Set offset percent for item alignment in addition to getItemAlignmentOffset().

Parameters
offsetPercent Percentage to offset. E.g., 40 means 40% of the width from the low edge. Use ITEM_ALIGN_OFFSET_PERCENT_DISABLED to disable.

public void setItemAlignmentOffsetWithPadding (boolean withPadding)

Set to true if include padding in calculating item align offset.

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

public void setItemAlignmentViewId (int viewId)

Set the id of the view to align with. Use zero (default) for the item view itself.

public void setItemMargin (int margin)

Set the margin in pixels between two child items.

public void setLayoutEnabled (boolean layoutEnabled)

Enable or disable layout. All children will be removed when layout is disabled.

public void setNumRows (int numRows)

Set the number of rows. Defaults to one.

public void setOnChildSelectedListener (OnChildSelectedListener listener)

Register a callback to be invoked when an item in BaseGridView has been selected.

public void setPruneChild (boolean pruneChild)

Enable or disable pruning child. Disable is useful during transition.

public void setRowHeight (int height)

Set the row height.

Parameters
height May be WRAP_CONTENT, or a size in pixels. If zero, row height will be fixed based on number of rows and view height.

public void setSelectedPosition (int position)

Change the selected item immediately without animation.

public void setSelectedPositionSmooth (int position)

Change the selected item and run an animation to scroll to the target position.

public void setVerticalMargin (int margin)

Set the margin in pixels between two child items vertically.

public void setWindowAlignment (int windowAlignment)

Set how the focused item gets aligned in the view.

public void setWindowAlignmentOffset (int offset)

Set the absolute offset in pixels for window alignment.

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

public void setWindowAlignmentOffsetPercent (float offsetPercent)

Set offset percent for window alignment in addition to getWindowAlignmentOffset().

Parameters
offsetPercent Percentage to offset. E.g., 40 means 40% of the width from low edge. Use WINDOW_ALIGN_OFFSET_PERCENT_DISABLED to disable.

Protected Methods

protected void initAttributes (Context context, AttributeSet attrs)

protected void initBaseGridViewAttributes (Context context, AttributeSet attrs)