Save the date! Android Dev Summit is coming to Sunnyvale, CA on Oct 23-24, 2019.

GridLayoutManager

open class GridLayoutManager : LinearLayoutManager
kotlin.Any
   ↳ androidx.recyclerview.widget.RecyclerView.LayoutManager
   ↳ androidx.recyclerview.widget.LinearLayoutManager
   ↳ androidx.recyclerview.widget.GridLayoutManager

A RecyclerView.LayoutManager implementations that lays out items in a grid.

By default, each item occupies 1 span. You can change it by providing a custom SpanSizeLookup instance via setSpanSizeLookup(SpanSizeLookup).

Summary

Nested classes
abstract

A helper class to provide the number of spans each item occupies.

Default implementation for SpanSizeLookup.

open

LayoutParams used by GridLayoutManager.

Constants
static Int

Inherited constants
Public constructors
<init>(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Constructor used when layout manager is set in XML by RecyclerView attribute "layoutManager".

<init>(context: Context!, spanCount: Int)

Creates a vertical GridLayoutManager

<init>(context: Context!, spanCount: Int, orientation: Int, reverseLayout: Boolean)

Public methods
open Unit
onItemsAdded(recyclerView: RecyclerView, positionStart: Int, itemCount: Int)

open Int

open Int

open Int

open Unit
setUsingSpansToEstimateScrollbarDimensions(useSpansToEstimateScrollBarDimentsions: Boolean)

When this flag is set, the scroll offset and scroll range calculations will take account of span information.

open Int

open RecyclerView.LayoutParams!

open RecyclerView.LayoutParams!

open Unit

open Unit
setStackFromEnd(stackFromEnd: Boolean)

stackFromEnd is not supported by GridLayoutManager.

open View?
onFocusSearchFailed(focused: View, focusDirection: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State)

open Boolean

open Unit
onItemsMoved(recyclerView: RecyclerView, from: Int, to: Int, itemCount: Int)

open Unit

Sets the source to get the number of spans occupied by each item in the adapter.

open Int

open Unit

open Unit
onItemsUpdated(recyclerView: RecyclerView, positionStart: Int, itemCount: Int, payload: Any?)

open Unit
setMeasuredDimension(childrenBounds: Rect!, wSpec: Int, hSpec: Int)

open Int

open Int

open Unit
onItemsRemoved(recyclerView: RecyclerView, positionStart: Int, itemCount: Int)

open Int

open Boolean

open GridLayoutManager.SpanSizeLookup!

Returns the current SpanSizeLookup used by the GridLayoutManager.

open Unit

open Unit
setSpanCount(spanCount: Int)

Sets the number of spans to be laid out.

open Int

Returns the number of spans laid out by this grid.

open RecyclerView.LayoutParams!

open Boolean

Returns true if the scroll offset and scroll range calculations take account of span information.

open Unit

Inherited functions

Constants

DEFAULT_SPAN_COUNT

static val DEFAULT_SPAN_COUNT: Int
Value: -1

Public constructors

<init>

GridLayoutManager(context: Context!, attrs: AttributeSet!, defStyleAttr: Int, defStyleRes: Int)

Constructor used when layout manager is set in XML by RecyclerView attribute "layoutManager". If spanCount is not specified in the XML, it defaults to a single column. androidx.recyclerview.R.attr#spanCount

<init>

GridLayoutManager(context: Context!, spanCount: Int)

Creates a vertical GridLayoutManager

Parameters
context Context!: Current context, will be used to access resources.
spanCount Context!: The number of columns in the grid

<init>

GridLayoutManager(context: Context!, spanCount: Int, orientation: Int, reverseLayout: Boolean)
Parameters
context Context!: Current context, will be used to access resources.
spanCount Context!: The number of columns or rows in the grid
orientation Context!: Layout orientation. Should be HORIZONTAL or .
reverseLayout Context!: When set to true, layouts from end to start.

Public methods

onItemsAdded

open fun onItemsAdded(recyclerView: RecyclerView, positionStart: Int, itemCount: Int): Unit

computeVerticalScrollRange

open fun computeVerticalScrollRange(state: RecyclerView.State): Int

computeHorizontalScrollRange

open fun computeHorizontalScrollRange(state: RecyclerView.State): Int

scrollHorizontallyBy

open fun scrollHorizontallyBy(dx: Int, recycler: RecyclerView.Recycler!, state: RecyclerView.State!): Int

setUsingSpansToEstimateScrollbarDimensions

open fun setUsingSpansToEstimateScrollbarDimensions(useSpansToEstimateScrollBarDimentsions: Boolean): Unit

When this flag is set, the scroll offset and scroll range calculations will take account of span information.

This is will increase the accuracy of the scroll bar's size and offset but will require more calls to SpanSizeLookup#getSpanGroupIndex(int, int)".

This additional accuracy may or may not be needed, depending on the characteristics of your layout. You will likely benefit from this accuracy when:

  • The variation in item span sizes is large.
  • The size of your data set is small (if your data set is large, the scrollbar will likely be very small anyway, and thus the increased accuracy has less impact).
  • Calls to SpanSizeLookup#getSpanGroupIndex(int, int) are fast.

If you decide to enable this feature, you should be sure that calls to SpanSizeLookup#getSpanGroupIndex(int, int) are fast, that set span group index caching is set to true via a call to and span index caching is also enabled via a call to.

scrollVerticallyBy

open fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler!, state: RecyclerView.State!): Int

generateLayoutParams

open fun generateLayoutParams(c: Context!, attrs: AttributeSet!): RecyclerView.LayoutParams!

generateLayoutParams

open fun generateLayoutParams(lp: LayoutParams!): RecyclerView.LayoutParams!

onInitializeAccessibilityNodeInfoForItem

open fun onInitializeAccessibilityNodeInfoForItem(recycler: RecyclerView.Recycler, state: RecyclerView.State, host: View, info: AccessibilityNodeInfoCompat): Unit

setStackFromEnd

open fun setStackFromEnd(stackFromEnd: Boolean): Unit

stackFromEnd is not supported by GridLayoutManager. Consider using setReverseLayout(boolean).

onFocusSearchFailed

open fun onFocusSearchFailed(focused: View, focusDirection: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): View?

supportsPredictiveItemAnimations

open fun supportsPredictiveItemAnimations(): Boolean

onItemsMoved

open fun onItemsMoved(recyclerView: RecyclerView, from: Int, to: Int, itemCount: Int): Unit

setSpanSizeLookup

open fun setSpanSizeLookup(spanSizeLookup: GridLayoutManager.SpanSizeLookup!): Unit

Sets the source to get the number of spans occupied by each item in the adapter.

Parameters
spanSizeLookup GridLayoutManager.SpanSizeLookup!: SpanSizeLookup instance to be used to query number of spans occupied by each item

getColumnCountForAccessibility

open fun getColumnCountForAccessibility(recycler: RecyclerView.Recycler, state: RecyclerView.State): Int

onItemsChanged

open fun onItemsChanged(recyclerView: RecyclerView): Unit

onItemsUpdated

open fun onItemsUpdated(recyclerView: RecyclerView, positionStart: Int, itemCount: Int, payload: Any?): Unit

setMeasuredDimension

open fun setMeasuredDimension(childrenBounds: Rect!, wSpec: Int, hSpec: Int): Unit

getRowCountForAccessibility

open fun getRowCountForAccessibility(recycler: RecyclerView.Recycler, state: RecyclerView.State): Int

computeVerticalScrollOffset

open fun computeVerticalScrollOffset(state: RecyclerView.State): Int

onItemsRemoved

open fun onItemsRemoved(recyclerView: RecyclerView, positionStart: Int, itemCount: Int): Unit

computeHorizontalScrollOffset

open fun computeHorizontalScrollOffset(state: RecyclerView.State): Int

checkLayoutParams

open fun checkLayoutParams(lp: RecyclerView.LayoutParams!): Boolean

getSpanSizeLookup

open fun getSpanSizeLookup(): GridLayoutManager.SpanSizeLookup!

Returns the current SpanSizeLookup used by the GridLayoutManager.

Return
GridLayoutManager.SpanSizeLookup!: The current SpanSizeLookup used by the GridLayoutManager.

onLayoutCompleted

open fun onLayoutCompleted(state: RecyclerView.State!): Unit

setSpanCount

open fun setSpanCount(spanCount: Int): Unit

Sets the number of spans to be laid out.

If getOrientation() is VERTICAL, this is the number of columns. If getOrientation() is HORIZONTAL, this is the number of rows.

Parameters
spanCount Int: The total number of spans in the grid

See Also

getSpanCount

open fun getSpanCount(): Int

Returns the number of spans laid out by this grid.

Return
Int: The number of spans

generateDefaultLayoutParams

open fun generateDefaultLayoutParams(): RecyclerView.LayoutParams!

isUsingSpansToEstimateScrollbarDimensions

open fun isUsingSpansToEstimateScrollbarDimensions(): Boolean

Returns true if the scroll offset and scroll range calculations take account of span information. See setUsingSpansToEstimateScrollbarDimensions(boolean) for more information on this topic.

Return
Boolean: true if the scroll offset and scroll range calculations take account of span information.

onLayoutChildren

open fun onLayoutChildren(recycler: RecyclerView.Recycler!, state: RecyclerView.State!): Unit