# LeanbackSpanSizeLookup

Artifact: [androidx.leanback:leanback-grid](https://developer.android.com/jetpack/androidx/releases/leanback) [View Source](https://cs.android.com/search?q=file:androidx/leanback/widget/LeanbackSpanSizeLookup.java+class:androidx.leanback.widget.LeanbackSpanSizeLookup) Added in [1.1.0-alpha02](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0-alpha02)

*** ** * ** ***


```
abstract class LeanbackSpanSizeLookup
```

<br />

*** ** * ** ***

A helper class to provide the number of spans each item occupies. `https://developer.android.com/reference/kotlin/androidx/leanback/widget/BaseGridView` retrieves the object from `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/RecyclerView.Adapter` via `https://developer.android.com/reference/kotlin/androidx/leanback/widget/FacetProvider` interface implemented by the adapter. The class is similar to `https://developer.android.com/reference/kotlin/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup`. It supports two special span size: `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS_AND_PADDINGS()` and `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS()`.

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS() = -2` Value returned by `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanSize(int)`: fill all spans. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS_AND_PADDINGS() = -1` Value returned by `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanSize(int)`: fill all spans and padding area, it takes full width of a vertical grid view or full height of a horizontal grid view. |

| ### Public constructors |
|---|
| `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#LeanbackSpanSizeLookup()()` |

| ### Public functions |
|---|---|
| `java-static https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getDefault()()` Return the default LeanbackSpanSizeLookup that each item has a span size of 1. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanGroupIndex(int,int)(adapterPosition: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, spanCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the index of the group this position belongs. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanIndex(int,int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, spanCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the final span index of the provided position. |
| `abstract https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanSize(int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the number of spans this item will take, or `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS_AND_PADDINGS()`, `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS()`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanSize(int,int)(position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html, spanCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html)` Returns the number of spans this item will take, or `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS_AND_PADDINGS()`. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#invalidateSpanGroupIndexCache()()` Clears the span group index cache. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#invalidateSpanIndexCache()()` Clears the span index cache. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#isSpanGroupIndexCacheEnabled()()` Returns whether results of `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanGroupIndex(int,int)` method are cached or not. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#isSpanIndexCacheEnabled()()` Returns whether results of `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanIndex(int,int)` method are cached or not. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#setSpanGroupIndexCacheEnabled(boolean)(cacheSpanGroupIndices: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Sets whether the results of `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanGroupIndex(int,int)` method should be cached or not. |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#setSpanIndexCacheEnabled(boolean)(cacheSpanIndices: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html)` Sets whether the results of `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanIndex(int,int)` method should be cached or not. |

## Constants

### FILL_ALL_SPANS

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

```
const val FILL_ALL_SPANS = -2: Int
```

Value returned by `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanSize(int)`: fill all spans. It's equivalent of returning the spanCount.

### FILL_ALL_SPANS_AND_PADDINGS

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

```
const val FILL_ALL_SPANS_AND_PADDINGS = -1: Int
```

Value returned by `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanSize(int)`: fill all spans and padding area, it takes full width of a vertical grid view or full height of a horizontal grid view.

## Public constructors

### LeanbackSpanSizeLookup

Added in 1.1.0-alpha02

```
LeanbackSpanSizeLookup()
```

## Public functions

### getDefault

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

```
java-static fun getDefault(): LeanbackSpanSizeLookup
```

Return the default LeanbackSpanSizeLookup that each item has a span size of 1.

### getSpanGroupIndex

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

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

Returns the index of the group this position belongs.

If `https://developer.android.com/reference/kotlin/androidx/leanback/widget/VerticalGridView`, this is a row value. If `https://developer.android.com/reference/kotlin/androidx/leanback/widget/HorizontalGridView`, 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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The position in adapter |
| `spanCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The total number of spans in the grid |

| Returns |
|---|---|
| `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.1.0-alpha02](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0-alpha02)

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

Returns the final span index of the provided position.

If `https://developer.android.com/reference/kotlin/androidx/leanback/widget/VerticalGridView`, this is a column value. If `https://developer.android.com/reference/kotlin/androidx/leanback/widget/HorizontalGridView`, 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 (`https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#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 `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#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: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The position of the item |
| `spanCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The total number of spans in the grid |

| Returns |
|---|---|
| `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.1.0-alpha02](https://developer.android.com/jetpack/androidx/releases/leanback#1.1.0-alpha02)

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

Returns the number of spans this item will take, or `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS_AND_PADDINGS()`, `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS()`.

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

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The number of spans this item will take, or `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS_AND_PADDINGS()`, `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS()`. |

### getSpanSize

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

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

Returns the number of spans this item will take, or `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS_AND_PADDINGS()`. Default implementation calls `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanSize(int)` without spanCount. Subclass may override this method to return span size based on spanCount.

| Parameters |
|---|---|
| `position: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The adapter position of the item. |
| `spanCount: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The total available spans of the grid. |

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | The number of spans this item will take, or `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#FILL_ALL_SPANS_AND_PADDINGS()`. |

### invalidateSpanGroupIndexCache

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

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

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

### invalidateSpanIndexCache

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

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

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

### isSpanGroupIndexCacheEnabled

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

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

Returns whether results of `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanGroupIndex(int,int)` method are cached or not.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | True if results of `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanGroupIndex(int,int)` are cached. |

### isSpanIndexCacheEnabled

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

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

Returns whether results of `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanIndex(int,int)` method are cached or not.

| Returns |
|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-boolean/index.html` | True if results of `https://developer.android.com/reference/kotlin/androidx/leanback/widget/LeanbackSpanSizeLookup#getSpanIndex(int,int)` are cached. |

### setSpanGroupIndexCacheEnabled

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

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

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

### setSpanIndexCacheEnabled

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

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

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

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