Class GridView

All Implemented Interfaces:
Drawable.Callback, ViewManager, ViewParent, Filter.FilterListener

public class GridView extends AbsListView
A view that shows items in two-dimensional scrolling grid. The items in the grid come from the ListAdapter associated with this view.
  • Field Details

  • Constructor Details

    • GridView

      public GridView(Context context)
  • Method Details

    • getAdapter

      public ListAdapter getAdapter()
      Description copied from class: AdapterView
      Returns the adapter currently associated with this widget.
      Specified by:
      getAdapter in class AdapterView<ListAdapter>
      Returns:
      The adapter used to provide this view's content.
    • setAdapter

      public void setAdapter(ListAdapter adapter)
      Sets the data behind this GridView.
      Overrides:
      setAdapter in class AbsListView
      Parameters:
      adapter - the adapter providing the grid's data
    • smoothScrollToPosition

      public void smoothScrollToPosition(int position)
      Smoothly scroll to the specified adapter position. The view will scroll such that the indicated position is displayed.
      Overrides:
      smoothScrollToPosition in class AbsListView
      Parameters:
      position - Scroll to this adapter position.
    • smoothScrollByOffset

      public void smoothScrollByOffset(int offset)
      Smoothly scroll to the specified adapter position offset. The view will scroll such that the indicated position is displayed.
      Parameters:
      offset - The amount to offset from the adapter position to scroll to.
    • onMeasure

      protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      Description copied from class: View
      Measure the view and its content to determine the measured width and the measured height. This method is invoked by View.measure(int, int) and should be overridden by subclasses to provide accurate and efficient measurement of their contents.

      CONTRACT: When overriding this method, you must call View.setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by View.measure(int, int). Calling super.onMeasure() is a valid use.

      The base class implementation of measure defaults to the background size, unless a larger size is allowed by the MeasureSpec. Subclasses should override the base one to provide better measurements of their content.

      Overrides:
      onMeasure in class AbsListView
      Parameters:
      widthMeasureSpec - width measure specification imposed by the parent MeasureSpec
      heightMeasureSpec - height measure specification imposed by the parent MeasureSpec
    • layoutChildren

      protected void layoutChildren()
      Description copied from class: AbsListView
      Subclasses must override this method to layout their children.
      Overrides:
      layoutChildren in class AbsListView
    • setSelection

      public void setSelection(int position)
      Sets the currently selected item
      Specified by:
      setSelection in class AdapterView<ListAdapter>
      Parameters:
      position - Index (starting at 0) of the data item to be selected.

      If in touch mode, the item will not be selected but it will still be positioned appropriately.

    • onKeyDown

      public boolean onKeyDown(int keyCode, KeyEvent event)
      Description copied from class: View
      Default implementation: perform press of the view when KeyEvent.KEY_ENTER, KeyEvent.KEY_KP_ENTER or KeyEvent.KEY_SPACE is released, if the view is enabled and clickable.
      Overrides:
      onKeyDown in class AbsListView
      Parameters:
      keyCode - a key code that represents the button pressed, from KeyEvent
      event - the KeyEvent object that defines the button action
    • onKeyMultiple

      public boolean onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
    • onKeyUp

      public boolean onKeyUp(int keyCode, KeyEvent event)
      Description copied from class: View
      Default implementation: perform clicking of the view when KeyEvent.KEY_ENTER, KeyEvent.KEY_KP_ENTER or KeyEvent.KEY_SPACE is released.
      Overrides:
      onKeyUp in class AbsListView
      Parameters:
      keyCode - A key code that represents the button pressed, from KeyEvent.
      event - The KeyEvent object that defines the button action.
    • onFocusChanged

      protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)
      Description copied from class: View
      Called by the view system when the focus state of this view changes. When the focus change event is caused by directional navigation, direction and previouslyFocusedRect provide insight into where the focus is coming from. When overriding, be sure to call up through to the super class so that the standard focus handling will occur.
      Overrides:
      onFocusChanged in class AbsListView
      Parameters:
      gainFocus - True if the View has focus; false otherwise.
      direction - The direction focus has moved when requestFocus() is called to give this view focus. Values are View.FOCUS_UP, View.FOCUS_DOWN, View.FOCUS_LEFT, View.FOCUS_RIGHT, View.FOCUS_FORWARD, or View.FOCUS_BACKWARD. It may not always apply, in which case use the default.
      previouslyFocusedRect - The rectangle, in this view's coordinate system, of the previously focused view. If applicable, this will be passed in as finer grained information about where the focus is coming from (in addition to direction). Will be null otherwise.
    • setGravity

      public void setGravity(int gravity)
      Set the gravity for this grid. Gravity describes how the child views are horizontally aligned. Defaults to Gravity.LEFT
      Parameters:
      gravity - the gravity to apply to this grid's children
    • getGravity

      public int getGravity()
      Describes how the child views are horizontally aligned. Defaults to Gravity.LEFT
      Returns:
      the gravity that will be applied to this grid's children
    • setHorizontalSpacing

      public void setHorizontalSpacing(int horizontalSpacing)
      Set the amount of horizontal (x) spacing to place between each item in the grid.
      Parameters:
      horizontalSpacing - The amount of horizontal space between items, in pixels.
    • getHorizontalSpacing

      public int getHorizontalSpacing()
      Returns the amount of horizontal spacing currently used between each item in the grid.

      This is only accurate for the current layout. If setHorizontalSpacing(int) has been called but layout is not yet complete, this method may return a stale value. To get the horizontal spacing that was explicitly requested use getRequestedHorizontalSpacing().

      Returns:
      Current horizontal spacing between each item in pixels
      See Also:
    • getRequestedHorizontalSpacing

      public int getRequestedHorizontalSpacing()
      Returns the requested amount of horizontal spacing between each item in the grid.

      The value returned may have been supplied during inflation as part of a style, the default GridView style, or by a call to setHorizontalSpacing(int). If layout is not yet complete or if GridView calculated a different horizontal spacing from what was requested, this may return a different value from getHorizontalSpacing().

      Returns:
      The currently requested horizontal spacing between items, in pixels
      See Also:
    • setVerticalSpacing

      public void setVerticalSpacing(int verticalSpacing)
      Set the amount of vertical (y) spacing to place between each item in the grid.
      Parameters:
      verticalSpacing - The amount of vertical space between items, in pixels.
      See Also:
    • getVerticalSpacing

      public int getVerticalSpacing()
      Returns the amount of vertical spacing between each item in the grid.
      Returns:
      The vertical spacing between items in pixels
      See Also:
    • setStretchMode

      public void setStretchMode(int stretchMode)
      Control how items are stretched to fill their space.
      Parameters:
      stretchMode - Either NO_STRETCH, STRETCH_SPACING, STRETCH_SPACING_UNIFORM, or STRETCH_COLUMN_WIDTH.
    • getStretchMode

      public int getStretchMode()
    • setColumnWidth

      public void setColumnWidth(int columnWidth)
      Set the width of columns in the grid.
      Parameters:
      columnWidth - The column width, in pixels.
    • getColumnWidth

      public int getColumnWidth()
      Return the width of a column in the grid.

      This may not be valid yet if a layout is pending.

      Returns:
      The column width in pixels
      See Also:
    • getRequestedColumnWidth

      public int getRequestedColumnWidth()
      Return the requested width of a column in the grid.

      This may not be the actual column width used. Use getColumnWidth() to retrieve the current real width of a column.

      Returns:
      The requested column width in pixels
      See Also:
    • setNumColumns

      public void setNumColumns(int numColumns)
      Set the number of columns in the grid
      Parameters:
      numColumns - The desired number of columns.
    • getNumColumns

      public int getNumColumns()
      Get the number of columns in the grid. Returns AUTO_FIT if the Grid has never been laid out.
      See Also:
    • computeVerticalScrollExtent

      protected int computeVerticalScrollExtent()
      Description copied from class: View

      Compute the vertical extent of the vertical scrollbar's thumb within the vertical range. This value is used to compute the length of the thumb within the scrollbar's track.

      The range is expressed in arbitrary units that must be the same as the units used by View.computeVerticalScrollRange() and View.computeVerticalScrollOffset().

      The default extent is the drawing height of this view.

      Overrides:
      computeVerticalScrollExtent in class AbsListView
      Returns:
      the vertical extent of the scrollbar's thumb
      See Also:
    • computeVerticalScrollOffset

      protected int computeVerticalScrollOffset()
      Description copied from class: View

      Compute the vertical offset of the vertical scrollbar's thumb within the horizontal range. This value is used to compute the position of the thumb within the scrollbar's track.

      The range is expressed in arbitrary units that must be the same as the units used by View.computeVerticalScrollRange() and View.computeVerticalScrollExtent().

      The default offset is the scroll offset of this view.

      Overrides:
      computeVerticalScrollOffset in class AbsListView
      Returns:
      the vertical offset of the scrollbar's thumb
      See Also:
    • computeVerticalScrollRange

      protected int computeVerticalScrollRange()
      Description copied from class: View

      Compute the vertical range that the vertical scrollbar represents.

      The range is expressed in arbitrary units that must be the same as the units used by View.computeVerticalScrollExtent() and View.computeVerticalScrollOffset().

      Overrides:
      computeVerticalScrollRange in class AbsListView
      Returns:
      the total vertical range represented by the vertical scrollbar

      The default range is the drawing height of this view.

      See Also: