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 SAVE_ALL_CHILD Save on screen views plus save off screen child views without any limitation.
int SAVE_LIMITED_CHILD Save on screen views plus save off screen child views states up to getSaveChildrenLimitNumber().
int SAVE_NO_CHILD Dont save states of any child views.
int SAVE_ON_SCREEN_CHILD Only save on screen child views, the states are lost when they become off screen.
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
boolean dispatchGenericFocusedEvent(MotionEvent event)
Dispatch a generic motion event to the currently focused view.
boolean dispatchKeyEvent(KeyEvent event)
Dispatch a key event to the next view on the focus path.
boolean dispatchTouchEvent(MotionEvent event)
Pass the touch screen motion event down to the target view, or this view if it is the target.
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.
final int getSaveChildrenLimitNumber()
final int getSaveChildrenPolicy()
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 offset in pixels for window alignment.
float getWindowAlignmentOffsetPercent()
Get offset percent for window alignment in addition to getWindowAlignmentOffset().
boolean hasOverlappingRendering()
Returns whether this View has content which overlaps.
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.
boolean isFocusDrawingOrderEnabled()
Returns true if default "focus draw at last" order rule is enabled.
final boolean isFocusSearchDisabled()
Return true if focus search is disabled.
boolean isItemAlignmentOffsetWithPadding()
Returns true if include padding in calculating item align offset.
boolean isScrollEnabled()
Returns true if scrolling is enabled.
boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect)
Look for a descendant to call requestFocus() on.
void onRtlPropertiesChanged(int layoutDirection)
Notify layout manager that layout directionality has been updated
void setAnimateChildLayout(boolean animateChildLayout)
Set if an animation should run when a child changes size or when adding or removing a child.
void setChildrenVisibility(int visibility)
Change and override children's visibility.
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 setFocusDrawingOrderEnabled(boolean enabled)
Enable or disable the default "focus draw at last" order rule.
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 setHasOverlappingRendering(boolean hasOverlapping)
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 setOnChildLaidOutListener(OnChildLaidOutListener listener)
Register a callback to be invoked when an item in BaseGridView has been laid out.
void setOnChildSelectedListener(OnChildSelectedListener listener)
Register a callback to be invoked when an item in BaseGridView has been selected.
void setOnKeyInterceptListener(BaseGridView.OnKeyInterceptListener listener)
Sets the key intercept listener.
void setOnMotionInterceptListener(BaseGridView.OnMotionInterceptListener listener)
Sets the generic motion intercept listener.
void setOnTouchInterceptListener(BaseGridView.OnTouchInterceptListener listener)
Sets the touch intercept listener.
void setPruneChild(boolean pruneChild)
Enable or disable pruning child.
void setRecyclerListener(RecyclerView.RecyclerListener listener)
Register a listener that will be notified whenever a child view is recycled.
void setRowHeight(int height)
Set the row height.
final void setSaveChildrenLimitNumber(int limitNumber)
Set limit number when getSaveChildrenPolicy() is SAVE_LIMITED_CHILD.
final void setSaveChildrenPolicy(int savePolicy)
Set policy for saving children.
void setScrollEnabled(boolean scrollEnabled)
Enable or disable scrolling.
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 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.support.v4.view.ScrollingView
From interface android.view.ViewParent
From interface android.view.ViewManager
From interface android.graphics.drawable.Drawable.Callback
From interface android.view.KeyEvent.Callback
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 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.

Constant Value: 3 (0x00000003)

public static final int SAVE_LIMITED_CHILD

Save on screen views plus save off screen child views states up to getSaveChildrenLimitNumber().

Constant Value: 2 (0x00000002)

public static final int SAVE_NO_CHILD

Dont save states of any child views.

Constant Value: 0 (0x00000000)

public static final int SAVE_ON_SCREEN_CHILD

Only save on screen child views, the states are lost when they become off screen.

Constant Value: 1 (0x00000001)

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.

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.

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.

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.

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 boolean dispatchGenericFocusedEvent (MotionEvent event)

Dispatch a generic motion event to the currently focused view.

Do not call this method directly. Call dispatchGenericMotionEvent(MotionEvent) instead.

Parameters
event The motion event to be dispatched.
Returns
  • True if the event was handled by the view, false otherwise.

public boolean dispatchKeyEvent (KeyEvent event)

Dispatch a key event to the next view on the focus path. This path runs from the top of the view tree down to the currently focused view. If this view has focus, it will dispatch to itself. Otherwise it will dispatch the next node down the focus path. This method also fires any key listeners.

Parameters
event The key event to be dispatched.
Returns
  • True if the event was handled, false otherwise.

public boolean dispatchTouchEvent (MotionEvent event)

Pass the touch screen motion event down to the target view, or this view if it is the target.

Parameters
event The motion event to be dispatched.
Returns
  • True if the event was handled by the view, false otherwise.

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 final int getSaveChildrenLimitNumber ()

Returns

public final int getSaveChildrenPolicy ()

Returns

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 offset in pixels for window alignment.

Returns
  • The number of pixels to offset. If the offset is positive, it is distance from low edge (see WINDOW_ALIGN_LOW_EDGE); if the offset is negative, the absolute value is distance from high edge (see WINDOW_ALIGN_HIGH_EDGE). Default value is 0.

public float getWindowAlignmentOffsetPercent ()

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

Returns

public boolean hasOverlappingRendering ()

Returns whether this View has content which overlaps.

This function, intended to be overridden by specific View types, is an optimization when alpha is set on a view. If rendering overlaps in a view with alpha < 1, that view is drawn to an offscreen buffer and then composited into place, which can be expensive. If the view has no overlapping rendering, the view can draw each primitive with the appropriate alpha value directly. An example of overlapping rendering is a TextView with a background image, such as a Button. An example of non-overlapping rendering is a TextView with no background, or an ImageView with only the foreground image. The default implementation returns true; subclasses should override if they have cases which can be optimized.

The current implementation of the saveLayer and saveLayerAlpha methods in Canvas necessitates that a View return true if it uses the methods internally without passing the CLIP_TO_LAYER_SAVE_FLAG.

Returns
  • true if the content in this view might overlap, false otherwise.

public boolean hasPreviousViewInSameRow (int position)

Returns true if the view at the given position has a same row sibling in front of it. This will return true if first item view is not created. So application should check in both OnChildSelectedListener and OnChildLaidOutListener.

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 boolean isFocusDrawingOrderEnabled ()

Returns true if default "focus draw at last" order rule is enabled.

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 isScrollEnabled ()

Returns true if scrolling is enabled.

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 onRtlPropertiesChanged (int layoutDirection)

Notify layout manager that layout directionality has been updated

Parameters
layoutDirection the direction of the layout

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 setChildrenVisibility (int visibility)

Change and override children's visibility.

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 setFocusDrawingOrderEnabled (boolean enabled)

Enable or disable the default "focus draw at last" order rule.

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_START.

Parameters
gravity See Gravity

public void setHasOverlappingRendering (boolean hasOverlapping)

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 setOnChildLaidOutListener (OnChildLaidOutListener listener)

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

Parameters
listener The listener to be invoked.

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

Parameters
listener The listener to be invoked.

public void setOnKeyInterceptListener (BaseGridView.OnKeyInterceptListener listener)

Sets the key intercept listener.

public void setOnMotionInterceptListener (BaseGridView.OnMotionInterceptListener listener)

Sets the generic motion intercept listener.

public void setOnTouchInterceptListener (BaseGridView.OnTouchInterceptListener listener)

Sets the touch intercept listener.

public void setPruneChild (boolean pruneChild)

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

public void setRecyclerListener (RecyclerView.RecyclerListener listener)

Register a listener that will be notified whenever a child view is recycled.

This listener will be called when a LayoutManager or the RecyclerView decides that a child view is no longer needed. If an application associates expensive or heavyweight data with item views, this may be a good place to release or free those resources.

Parameters
listener Listener to register, or null to clear

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 final void setSaveChildrenLimitNumber (int limitNumber)

Set limit number when getSaveChildrenPolicy() is SAVE_LIMITED_CHILD.

public final void setSaveChildrenPolicy (int savePolicy)

Set policy for saving children.

public void setScrollEnabled (boolean scrollEnabled)

Enable or disable scrolling. Disable is useful during transition.

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 offset in pixels for window alignment.

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

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. Default value is 50.

Protected Methods

protected void initAttributes (Context context, AttributeSet attrs)

protected void initBaseGridViewAttributes (Context context, AttributeSet attrs)