public abstract class

GridLayoutManager.SpanSizeLookup

extends java.lang.Object

 java.lang.Object

↳androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup

Subclasses:

GridLayoutManager.DefaultSpanSizeLookup

Overview

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

Default implementation sets each item to occupy exactly 1 span.

Summary

Constructors
publicSpanSizeLookup()

Methods
public intgetSpanGroupIndex(int adapterPosition, int spanCount)

Returns the index of the group this position belongs.

public intgetSpanIndex(int position, int spanCount)

Returns the final span index of the provided position.

public abstract intgetSpanSize(int position)

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

public voidinvalidateSpanGroupIndexCache()

Clears the span group index cache.

public voidinvalidateSpanIndexCache()

Clears the span index cache.

public booleanisSpanGroupIndexCacheEnabled()

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

public booleanisSpanIndexCacheEnabled()

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

public voidsetSpanGroupIndexCacheEnabled(boolean cacheSpanGroupIndices)

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

public voidsetSpanIndexCacheEnabled(boolean cacheSpanIndices)

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

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Constructors

public SpanSizeLookup()

Methods

public abstract int getSpanSize(int position)

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

Parameters:

position: The adapter position of the item

Returns:

The number of spans occupied by the item at the provided position

public void setSpanIndexCacheEnabled(boolean cacheSpanIndices)

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

Parameters:

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

public void setSpanGroupIndexCacheEnabled(boolean cacheSpanGroupIndices)

Sets whether the results of GridLayoutManager.SpanSizeLookup.getSpanGroupIndex(int, int) method should be cached or not. By default these values are not cached. If you are not overriding 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: Whether results of getGroupSpanIndex should be cached or not.

public void invalidateSpanIndexCache()

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

public void invalidateSpanGroupIndexCache()

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

public boolean isSpanIndexCacheEnabled()

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

Returns:

True if results of GridLayoutManager.SpanSizeLookup.getSpanIndex(int, int) are cached.

public boolean isSpanGroupIndexCacheEnabled()

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

Returns:

True if results of GridLayoutManager.SpanSizeLookup.getSpanGroupIndex(int, int) are cached.

public int getSpanIndex(int position, int spanCount)

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 (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 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: The position of the item
spanCount: The total number of spans in the grid

Returns:

The final span position of the item. Should be between 0 (inclusive) and spanCount(exclusive)

public int getSpanGroupIndex(int adapterPosition, int spanCount)

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: The position in adapter
spanCount: The total number of spans in the grid

Returns:

The index of the span group including the item at the given adapter position