# GridLayoutManager.DefaultSpanSizeLookup

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.DefaultSpanSizeLookup) Added in [1.0.0](https://developer.android.com/jetpack/androidx/releases/recyclerview#1.0.0)

*** ** * ** ***


```
public final class GridLayoutManager.DefaultSpanSizeLookup extends GridLayoutManager.SpanSizeLookup
```

<br />

|---|---|---|
| [java.lang.Object](https://developer.android.com/reference/java/lang/Object.html) |||
| ↳ | [androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup](https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup) ||
|   | ↳ | [androidx.recyclerview.widget.GridLayoutManager.DefaultSpanSizeLookup](https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.DefaultSpanSizeLookup) |

*** ** * ** ***

Default implementation for `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup`. Each item occupies 1 span.

## Summary

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

| ### Public methods |
|---|---|
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.DefaultSpanSizeLookup#getSpanIndex(int,int)(int position, int spanCount)` Returns the final span index of the provided position. |
| `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.DefaultSpanSizeLookup#getSpanSize(int)(int position)` Returns the number of span occupied by the item at `position`. |

| ### Inherited methods |
|---|
| From [androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup](https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup) |---|---| | `int` | `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanGroupIndex(int,int)(int adapterPosition, int spanCount)` Returns the index of the group this position belongs. | | `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#invalidateSpanGroupIndexCache()()` Clears the span group index cache. | | `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#invalidateSpanIndexCache()()` Clears the span index cache. | | `boolean` | `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#isSpanGroupIndexCacheEnabled()()` Returns whether results of `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanGroupIndex(int,int)` method are cached or not. | | `boolean` | `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#isSpanIndexCacheEnabled()()` Returns whether results of `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanIndex(int,int)` method are cached or not. | | `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#setSpanGroupIndexCacheEnabled(boolean)(boolean cacheSpanGroupIndices)` Sets whether the results of `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanGroupIndex(int,int)` method should be cached or not. | | `void` | `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#setSpanIndexCacheEnabled(boolean)(boolean cacheSpanIndices)` Sets whether the results of `https://developer.android.com/reference/androidx/recyclerview/widget/GridLayoutManager.SpanSizeLookup#getSpanIndex(int,int)` method should be cached or not. | |

## Public constructors

### DefaultSpanSizeLookup

Added in 1.0.0

```
public DefaultSpanSizeLookup()
```

## Public methods

### getSpanIndex

```
public int getSpanIndex(int position, int spanCount)
```

Returns the final span index of the provided position.

If `https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager#getOrientation()` is `https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager#VERTICAL()`, this is a column value. If `https://developer.android.com/reference/androidx/recyclerview/widget/LinearLayoutManager#getOrientation()` is `https://developer.android.com/reference/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 (`https://developer.android.com/reference/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 `https://developer.android.com/reference/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 |
|---|---|
| `int position` | The position of the item |
| `int spanCount` | The total number of spans in the grid |

| Returns |
|---|---|
| `int` | The final span position of the item. Should be between 0 (inclusive) and `spanCount`(exclusive) |

### getSpanSize

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

```
public int getSpanSize(int position)
```

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

| Parameters |
|---|---|
| `int position` | The adapter position of the item |

| Returns |
|---|---|
| `int` | The number of spans occupied by the item at the provided position |