Android
android.widget
public class

android.widget.GridView

java.lang.Object
android.view.View Drawable.Callback KeyEvent.Callback
android.view.ViewGroup ViewManager ViewParent
android.widget.AdapterView<T extends android.widget.Adapter>
android.widget.AbsListView TextWatcher ViewTreeObserver.OnGlobalLayoutListener ViewTreeObserver.OnTouchModeChangeListener Filter.FilterListener
android.widget.GridView

A view that shows items in two-dimensional scrolling grid. The items in the grid come from the ListAdapter associated with this view.

Summary

XML Attributes

Attribute name Related methods  
android:columnWidth setColumnWidth(int)
 
 
android:gravity setGravity(int)
 
Specifies how to place an object, both its x and y axis, within a larger containing object. 
android:horizontalSpacing setHorizontalSpacing(int)
 
 
android:numColumns setNumColumns(int)
 
 
android:stretchMode setStretchMode(int)
 
 
android:verticalSpacing setVerticalSpacing(int)
 
 
XML Attributes inherited from class android.widget.AbsListView
XML Attributes inherited from class android.view.ViewGroup
XML Attributes inherited from class android.view.View

Constants

      Value  
int  AUTO_FIT    -1  0xffffffff 
int  NO_STRETCH    0x00000000 
int  STRETCH_COLUMN_WIDTH    0x00000002 
int  STRETCH_SPACING    0x00000001 
Constants inherited from class android.widget.AbsListView
Constants inherited from class android.widget.AdapterView
Constants inherited from class android.view.ViewGroup
Constants inherited from class android.view.View

Public Constructors

            GridView(Context context)
            GridView(Context context, AttributeSet attrs)
            GridView(Context context, AttributeSet attrs, int defStyle)

Public Methods

          ListAdapter  getAdapter()
Returns the adapter currently associated with this widget.
          int  getStretchMode()
          boolean  onKeyDown(int keyCode, KeyEvent event)
Default implementation of KeyEvent.Callback.onKeyMultiple(): perform press of the view when KEYCODE_DPAD_CENTER or KEYCODE_ENTER is released, if the view is enabled and clickable.
          boolean  onKeyMultiple(int keyCode, int repeatCount, KeyEvent event)
Default implementation of KeyEvent.Callback.onKeyMultiple(): always returns false (doesn't handle the event).
          boolean  onKeyUp(int keyCode, KeyEvent event)
Default implementation of KeyEvent.Callback.onKeyMultiple(): perform clicking of the view when KEYCODE_DPAD_CENTER or KEYCODE_ENTER is released.
          void  setAdapter(ListAdapter adapter)
Sets the data behind this GridView.
          void  setColumnWidth(int columnWidth)
Set the width of columns in the grid.
          void  setGravity(int gravity)
Describes how the child views are horizontally aligned.
          void  setHorizontalSpacing(int horizontalSpacing)
Set the amount of horizontal (x) spacing to place between each item in the grid.
          void  setNumColumns(int numColumns)
Set the number of columns in the grid
          void  setSelection(int position)
Sets the currently selected item
          void  setStretchMode(int stretchMode)
Control how items are stretched to fill their space.
          void  setVerticalSpacing(int verticalSpacing)
Set the amount of vertical (y) spacing to place between each item in the grid.

Protected Methods

          void  attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count)
Subclasses should override this method to set layout animation parameters on the supplied child.
          int  computeVerticalScrollExtent()

Compute the vertical extent of the horizontal scrollbar's thumb within the vertical range.

          int  computeVerticalScrollOffset()

Compute the vertical offset of the vertical scrollbar's thumb within the horizontal range.

          int  computeVerticalScrollRange()

Compute the vertical range that the vertical scrollbar represents.

          void  layoutChildren()
          void  onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)
Called by the view system when the focus state of this view changes.
          void  onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height.

Methods inherited from class android.widget.AbsListView
Methods inherited from class android.widget.AdapterView
Methods inherited from class android.view.ViewGroup
Methods inherited from class android.view.View
Methods inherited from class java.lang.Object
Methods inherited from interface android.graphics.drawable.Drawable.Callback
Methods inherited from interface android.text.TextWatcher
Methods inherited from interface android.view.KeyEvent.Callback
Methods inherited from interface android.view.ViewManager
Methods inherited from interface android.view.ViewParent
Methods inherited from interface android.view.ViewTreeObserver.OnGlobalLayoutListener
Methods inherited from interface android.view.ViewTreeObserver.OnTouchModeChangeListener
Methods inherited from interface android.widget.Filter.FilterListener

Details

XML Attributes

android:columnWidth

Related Methods

android:gravity

Specifies how to place an object, both its x and y axis, within a larger containing object.

Must be one or more (separated by '|') of the following constant values.

ConstantValueDescription
top0x30 Push object to the top of its container, not changing its size.
bottom0x50 Push object to the bottom of its container, not changing its size.
left0x03 Push object to the left of its container, not changing its size.
right0x05 Push object to the right of its container, not changing its size.
center_vertical0x10 Place object in the vertical center of its container, not changing its size.
fill_vertical0x70 Grow the vertical size of the object if needed so it completely fills its container.
center_horizontal0x01 Place object in the horizontal center of its container, not changing its size.
fill_horizontal0x07 Grow the horizontal size of the object if needed so it completely fills its container.
center0x11 Place the object in the center of its container in both the vertical and horizontal axis, not changing its size.
fill0x77 Grow the horizontal and vertical size of the object if needed so it completely fills its container.

This corresponds to the global attribute resource symbol gravity.

Related Methods

android:horizontalSpacing

Related Methods

android:numColumns

Related Methods

android:stretchMode

Related Methods

android:verticalSpacing

Related Methods

Constants

public static final int AUTO_FIT

Constant Value: -1 (0xffffffff)

public static final int NO_STRETCH

Constant Value: 0 (0x00000000)

public static final int STRETCH_COLUMN_WIDTH

Constant Value: 2 (0x00000002)

public static final int STRETCH_SPACING

Constant Value: 1 (0x00000001)

Public Constructors

public GridView(Context context)

public GridView(Context context, AttributeSet attrs)

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

Public Methods

public ListAdapter getAdapter()

Returns the adapter currently associated with this widget.

Returns

  • The adapter used to provide this view's content.

public int getStretchMode()

public boolean onKeyDown(int keyCode, KeyEvent event)

Default implementation of KeyEvent.Callback.onKeyMultiple(): perform press of the view when KEYCODE_DPAD_CENTER or KEYCODE_ENTER is released, if the view is enabled and clickable.

Parameters

keyCode A key code that represents the button pressed, from KeyEvent.
event The KeyEvent object that defines the button action.

Returns

  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

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

Default implementation of KeyEvent.Callback.onKeyMultiple(): always returns false (doesn't handle the event).

Parameters

keyCode A key code that represents the button pressed, from KeyEvent.
repeatCount The number of times the action was made.
event The KeyEvent object that defines the button action.

Returns

  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

public boolean onKeyUp(int keyCode, KeyEvent event)

Default implementation of KeyEvent.Callback.onKeyMultiple(): perform clicking of the view when KEYCODE_DPAD_CENTER or KEYCODE_ENTER is released.

Parameters

keyCode A key code that represents the button pressed, from KeyEvent.
event The KeyEvent object that defines the button action.

Returns

  • If you handled the event, return true. If you want to allow the event to be handled by the next receiver, return false.

public void setAdapter(ListAdapter adapter)

Sets the data behind this GridView.

Parameters

adapter the adapter providing the grid's data

public void setColumnWidth(int columnWidth)

Set the width of columns in the grid.

Related XML Attributes

Parameters

columnWidth The column width, in pixels.

public void setGravity(int gravity)

Describes how the child views are horizontally aligned. Defaults to Gravity.LEFT

Related XML Attributes

Parameters

gravity the gravity to apply to this grid's children

public void setHorizontalSpacing(int horizontalSpacing)

Set the amount of horizontal (x) spacing to place between each item in the grid.

Related XML Attributes

Parameters

horizontalSpacing The amount of horizontal space between items, in pixels.

public void setNumColumns(int numColumns)

Set the number of columns in the grid

Related XML Attributes

Parameters

numColumns The desired number of columns.

public void setSelection(int position)

Sets the currently selected item

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.

public void setStretchMode(int stretchMode)

Control how items are stretched to fill their space.

Related XML Attributes

Parameters

stretchMode Either NO_STRETCH, STRETCH_SPACING, or STRETCH_COLUMN_WIDTH.

public void setVerticalSpacing(int verticalSpacing)

Set the amount of vertical (y) spacing to place between each item in the grid.

Related XML Attributes

Parameters

verticalSpacing The amount of vertical space between items, in pixels.

Protected Methods

protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count)

Subclasses should override this method to set layout animation parameters on the supplied child.

Parameters

child the child to associate with animation parameters
params the child's layout parameters which hold the animation parameters
index the index of the child in the view group
count the number of children in the view group

protected int computeVerticalScrollExtent()

Compute the vertical extent of the horizontal 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 computeHorizontalScrollRange() and computeVerticalScrollOffset().

The default extent is the drawing height of this view.

Returns

  • the vertical extent of the scrollbar's thumb

protected int computeVerticalScrollOffset()

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 computeVerticalScrollRange() and computeVerticalScrollExtent().

The default offset is the scroll offset of this view.

Returns

  • the vertical offset of the scrollbar's thumb

protected int computeVerticalScrollRange()

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 computeVerticalScrollExtent() and computeVerticalScrollOffset().

Returns

  • the total vertical range represented by the vertical scrollbar

    The default range is the drawing height of this view.

protected void layoutChildren()

protected void onFocusChanged(boolean gainFocus, int direction, Rect previouslyFocusedRect)

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.

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 or View.FOCUS_RIGHT. 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.

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

Measure the view and its content to determine the measured width and the measured height. This method is invoked by measure(int, int) and should be overriden by subclasses to provide accurate and efficient measurement of their contents.

CONTRACT: When overriding this method, you must call setMeasuredDimension(int, int) to store the measured width and height of this view. Failure to do so will trigger an IllegalStateException, thrown by measure(int, int). Calling the superclass' onMeasure(int, int) 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 onMeasure(int, int) to provide better measurements of their content.

If this method is overridden, it is the subclass's responsibility to make sure the measured height and width are at least the view's minimum height and width (getSuggestedMinimumHeight() and getSuggestedMinimumWidth()).

Parameters

widthMeasureSpec horizontal space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
heightMeasureSpec vertical space requirements as imposed by the parent. The requirements are encoded with View.MeasureSpec.
Copyright 2007 Google Inc. Build 119366-119366 - 19 Nov 2008 16:04