The Android Developer Challenge is back! Submit your idea before December 2.

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

Default implementation for SpanSizeLookup.

open

LayoutParams used by GridLayoutManager.

abstract

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

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 Boolean

open Int

open Int

open Int

open Int

open RecyclerView.LayoutParams!

open RecyclerView.LayoutParams!

open RecyclerView.LayoutParams!

open Int

open Int

open Int

Returns the number of spans laid out by this grid.

open GridLayoutManager.SpanSizeLookup!

Returns the current SpanSizeLookup used by the GridLayoutManager.

open Boolean

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

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

open Unit

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

open Unit

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

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

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

open Unit

open Unit

open Int

open Int

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

open Unit
setSpanCount(spanCount: Int)

Sets the number of spans to be laid out.

open Unit

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

open Unit
setStackFromEnd(stackFromEnd: Boolean)

stackFromEnd is not supported by GridLayoutManager.

open Unit
setUsingSpansToEstimateScrollbarDimensions(useSpansToEstimateScrollBarDimensions: Boolean)

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

open Boolean

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

checkLayoutParams

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

computeHorizontalScrollOffset

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

computeHorizontalScrollRange

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

computeVerticalScrollOffset

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

computeVerticalScrollRange

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

generateDefaultLayoutParams

open fun generateDefaultLayoutParams(): RecyclerView.LayoutParams!

generateLayoutParams

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

generateLayoutParams

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

getColumnCountForAccessibility

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

getRowCountForAccessibility

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

getSpanCount

open fun getSpanCount(): Int

Returns the number of spans laid out by this grid.

Return
Int: The number of spans

getSpanSizeLookup

open fun getSpanSizeLookup(): GridLayoutManager.SpanSizeLookup!

Returns the current SpanSizeLookup used by the GridLayoutManager.

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

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. Defaults to false.

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

onFocusSearchFailed

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

onInitializeAccessibilityNodeInfoForItem

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

onItemsAdded

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

onItemsChanged

open fun onItemsChanged(recyclerView: RecyclerView): Unit

onItemsMoved

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

onItemsRemoved

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

onItemsUpdated

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

onLayoutChildren

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

onLayoutCompleted

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

scrollHorizontallyBy

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

scrollVerticallyBy

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

setMeasuredDimension

open fun setMeasuredDimension(childrenBounds: Rect!, wSpec: Int, hSpec: Int): 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

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

setStackFromEnd

open fun setStackFromEnd(stackFromEnd: Boolean): Unit

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

setUsingSpansToEstimateScrollbarDimensions

open fun setUsingSpansToEstimateScrollbarDimensions(useSpansToEstimateScrollBarDimensions: 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.

supportsPredictiveItemAnimations

open fun supportsPredictiveItemAnimations(): Boolean