Android Dev Summit, October 23-24: two days of technical content, directly from the Android team. Sign-up for livestream updates.

SpanSizeLookup

abstract class SpanSizeLookup
kotlin.Any
   ↳ androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup

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

Default implementation sets each item to occupy exactly 1 span.

Summary

Public constructors

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

Public methods

open Int
getSpanGroupIndex(adapterPosition: Int, spanCount: Int)

Returns the index of the group this position belongs.

open Int
getSpanIndex(position: Int, spanCount: Int)

Returns the final span index of the provided position.

abstract Int
getSpanSize(position: Int)

Returns the number of span occupied by the item at position.

open Unit

Clears the span group index cache.

open Unit

Clears the span index cache.

open Boolean

Returns whether results of getSpanGroupIndex(int, int) method are cached or not.

open Boolean

Returns whether results of getSpanIndex(int, int) method are cached or not.

open Unit
setSpanGroupIndexCacheEnabled(cacheSpanGroupIndices: Boolean)

Sets whether the results of getSpanGroupIndex(int, int) method should be cached or not.

open Unit
setSpanIndexCacheEnabled(cacheSpanIndices: Boolean)

Sets whether the results of getSpanIndex(int, int) method should be cached or not.

Public constructors

<init>

SpanSizeLookup()

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

Default implementation sets each item to occupy exactly 1 span.

Public methods

getSpanGroupIndex

open fun getSpanGroupIndex(adapterPosition: Int, spanCount: Int): Int

Returns the index of the group this position belongs.

For example, if grid has 3 columns and each item occupies 1 span, span group index for item 1 will be 0, item 5 will be 1.

Parameters
adapterPosition Int: The position in adapter
spanCount Int: The total number of spans in the grid
Return
Int: The index of the span group including the item at the given adapter position

getSpanIndex

open fun getSpanIndex(position: Int, spanCount: Int): Int

Returns the final span index of the provided position.

If you have a faster way to calculate span index for your items, you should override this method. Otherwise, you should enable span index cache (setSpanIndexCacheEnabled(boolean)) for better performance. When caching is disabled, default implementation traverses all items from 0 to position. When caching is enabled, it calculates from the closest cached value before the position.

If you override this method, you need to make sure it is consistent with getSpanSize(int). GridLayoutManager does not call this method for each item. It is called only for the reference item and rest of the items are assigned to spans based on the reference item. For example, you cannot assign a position to span 2 while span 1 is empty.

Note that span offsets always start with 0 and are not affected by RTL.

Parameters
position Int: The position of the item
spanCount Int: The total number of spans in the grid
Return
Int: The final span position of the item. Should be between 0 (inclusive) and spanCount(exclusive)

getSpanSize

abstract fun getSpanSize(position: Int): Int

Returns the number of span occupied by the item at position.

Parameters
position Int: The adapter position of the item
Return
Int: The number of spans occupied by the item at the provided position

invalidateSpanGroupIndexCache

open fun invalidateSpanGroupIndexCache(): Unit

Clears the span group index cache. GridLayoutManager automatically calls this method when adapter changes occur.

invalidateSpanIndexCache

open fun invalidateSpanIndexCache(): Unit

Clears the span index cache. GridLayoutManager automatically calls this method when adapter changes occur.

isSpanGroupIndexCacheEnabled

open fun isSpanGroupIndexCacheEnabled(): Boolean

Returns whether results of getSpanGroupIndex(int, int) method are cached or not.

Return
Boolean: True if results of getSpanGroupIndex(int, int) are cached.

isSpanIndexCacheEnabled

open fun isSpanIndexCacheEnabled(): Boolean

Returns whether results of getSpanIndex(int, int) method are cached or not.

Return
Boolean: True if results of getSpanIndex(int, int) are cached.

setSpanGroupIndexCacheEnabled

open fun setSpanGroupIndexCacheEnabled(cacheSpanGroupIndices: Boolean): Unit

Sets whether the results of getSpanGroupIndex(int, int) method should be cached or not. By default these values are not cached. If you are not overriding getSpanGroupIndex(int, int) with something highly performant, and you are using spans to calculate scrollbar offset and range, you should set this to true for better performance.

Parameters
cacheSpanGroupIndices Boolean: Whether results of getGroupSpanIndex should be cached or not.

setSpanIndexCacheEnabled

open fun setSpanIndexCacheEnabled(cacheSpanIndices: Boolean): Unit

Sets whether the results of getSpanIndex(int, int) method should be cached or not. By default these values are not cached. If you are not overriding getSpanIndex(int, int) with something highly performant, you should set this to true for better performance.

Parameters
cacheSpanIndices Boolean: Whether results of getSpanIndex should be cached or not.