# GridLayoutManager.SpanSizeLookup

# GridLayoutManager.SpanSizeLookup

Artifact:[androidx.recyclerview:recyclerview](https://developer.android.com/jetpack/androidx/releases/recyclerview)  
[View Source](https://cs.android.com/search?q=file:androidx/recyclerview/widget/GridLayoutManager.java+class:androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup)  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup "View this page in Java")

<br />

```
abstract class GridLayoutManager.SpanSizeLookup
```

<br />

Known direct subclasses  
[GridLayoutManager.DefaultSpanSizeLookup](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.DefaultSpanSizeLookup)  

|----------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------|
| [GridLayoutManager.DefaultSpanSizeLookup](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.DefaultSpanSizeLookup) | Default implementation for[SpanSizeLookup](https://developer.android.com/reference/kotlin/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.  

|                                                                                              See also                                                                                               |
|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [setSpanSizeLookup](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager#setSpanSizeLookup(androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup)) |   |

## Summary

|                                                               ### Public constructors                                                               |
|-----------------------------------------------------------------------------------------------------------------------------------------------------|
| [SpanSizeLookup](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#SpanSizeLookup())`()` |

|                                  ### Public functions                                  |
|----------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)            | [getSpanGroupIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanGroupIndex(int,int))`(adapterPosition: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, spanCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the index of the group this position belongs.                                                                                                              |
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)            | [getSpanIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanIndex(int,int))`(position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`, spanCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the final span index of the provided position.                                                                                                                              |
| `abstract `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | [getSpanSize](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanSize(int))`(position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)`)` Returns the number of span occupied by the item at`position`.                                                                                                                                                                                                                       |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)          | [invalidateSpanGroupIndexCache](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#invalidateSpanGroupIndexCache())`()` Clears the span group index cache.                                                                                                                                                                                                                                                                                                        |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)          | [invalidateSpanIndexCache](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#invalidateSpanIndexCache())`()` Clears the span index cache.                                                                                                                                                                                                                                                                                                                        |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)    | [isSpanGroupIndexCacheEnabled](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#isSpanGroupIndexCacheEnabled())`()` Returns whether results of[getSpanGroupIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanGroupIndex(int,int))method are cached or not.                                                                                                                             |
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)    | [isSpanIndexCacheEnabled](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#isSpanIndexCacheEnabled())`()` Returns whether results of[getSpanIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanIndex(int,int))method are cached or not.                                                                                                                                                 |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)          | [setSpanGroupIndexCacheEnabled](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#setSpanGroupIndexCacheEnabled(boolean))`(cacheSpanGroupIndices: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`)` Sets whether the results of[getSpanGroupIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanGroupIndex(int,int))method should be cached or not. |
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)          | [setSpanIndexCacheEnabled](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#setSpanIndexCacheEnabled(boolean))`(cacheSpanIndices: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)`)` Sets whether the results of[getSpanIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanIndex(int,int))method should be cached or not.                          |

## Public constructors

### SpanSizeLookup

Added in 1.0.0  

```
SpanSizeLookup()
```  

## Public functions

### getSpanGroupIndex

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

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

Returns the index of the group this position belongs.

If[getOrientation](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/LinearLayoutManager#getOrientation())is[VERTICAL](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/LinearLayoutManager#VERTICAL()), this is a row value. If[getOrientation](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/LinearLayoutManager#getOrientation())is[HORIZONTAL](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/LinearLayoutManager#HORIZONTAL()), this is a column value.

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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The position in adapter               |
| `spanCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)       | The total number of spans in the grid |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The index of the span group including the item at the given adapter position |

### getSpanIndex

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

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

Returns the final span index of the provided position.

If[getOrientation](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/LinearLayoutManager#getOrientation())is[VERTICAL](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/LinearLayoutManager#VERTICAL()), this is a column value. If[getOrientation](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/LinearLayoutManager#getOrientation())is[HORIZONTAL](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/LinearLayoutManager#HORIZONTAL()), this is a row value.

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](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#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](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)  | The position of the item              |
| `spanCount: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The total number of spans in the grid |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|------------------------------------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The final span position of the item. Should be between 0 (inclusive) and`spanCount`(exclusive) |

### getSpanSize

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
abstract fun getSpanSize(position: Int): Int
```

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

|                                       Parameters                                        |
|-----------------------------------------------------------------------------------------|----------------------------------|
| `position: `[Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The adapter position of the item |

|                                   Returns                                   |
|-----------------------------------------------------------------------------|-------------------------------------------------------------------|
| [Int](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html) | The number of spans occupied by the item at the provided position |

### invalidateSpanGroupIndexCache

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.1.0)  

```
fun invalidateSpanGroupIndexCache(): Unit
```

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

### invalidateSpanIndexCache

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
fun invalidateSpanIndexCache(): Unit
```

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

### isSpanGroupIndexCacheEnabled

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.1.0)  

```
fun isSpanGroupIndexCacheEnabled(): Boolean
```

Returns whether results of[getSpanGroupIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanGroupIndex(int,int))method are cached or not.  

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | True if results of[getSpanGroupIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanGroupIndex(int,int))are cached. |

### isSpanIndexCacheEnabled

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
fun isSpanIndexCacheEnabled(): Boolean
```

Returns whether results of[getSpanIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanIndex(int,int))method are cached or not.  

|                                       Returns                                       |
|-------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | True if results of[getSpanIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanIndex(int,int))are cached. |

### setSpanGroupIndexCacheEnabled

Added in[1.1.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.1.0)  

```
fun setSpanGroupIndexCacheEnabled(cacheSpanGroupIndices: Boolean): Unit
```

Sets whether the results of[getSpanGroupIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanGroupIndex(int,int))method should be cached or not. By default these values are not cached. If you are not overriding[getSpanGroupIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#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](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | Whether results of getGroupSpanIndex should be cached or not. |

### setSpanIndexCacheEnabled

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)  

```
fun setSpanIndexCacheEnabled(cacheSpanIndices: Boolean): Unit
```

Sets whether the results of[getSpanIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanIndex(int,int))method should be cached or not. By default these values are not cached. If you are not overriding[getSpanIndex](https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanIndex(int,int))with something highly performant, you should set this to true for better performance.  

|                                               Parameters                                                |
|---------------------------------------------------------------------------------------------------------|----------------------------------------------------------|
| `cacheSpanIndices: `[Boolean](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html) | Whether results of getSpanIndex should be cached or not. |