LeanbackSpanSizeLookup


public abstract class LeanbackSpanSizeLookup


A helper class to provide the number of spans each item occupies. BaseGridView retrieves the object from androidx.recyclerview.widget.RecyclerView.Adapter via FacetProvider interface implemented by the adapter. The class is similar to androidx.recyclerview.widget.GridLayoutManager.SpanSizeLookup. It supports two special span size: FILL_ALL_SPANS_AND_PADDINGS and FILL_ALL_SPANS.

Summary

Constants

static final int

Value returned by getSpanSize: fill all spans.

static final int

Value returned by getSpanSize: 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

Public methods

static @NonNull LeanbackSpanSizeLookup

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

int
getSpanGroupIndex(int adapterPosition, int spanCount)

Returns the index of the group this position belongs.

int
getSpanIndex(int position, int spanCount)

Returns the final span index of the provided position.

abstract int
getSpanSize(int position)

Returns the number of spans this item will take, or FILL_ALL_SPANS_AND_PADDINGS, FILL_ALL_SPANS.

int
getSpanSize(int position, int spanCount)

Returns the number of spans this item will take, or FILL_ALL_SPANS_AND_PADDINGS.

void

Clears the span group index cache.

void

Clears the span index cache.

boolean

Returns whether results of getSpanGroupIndex method are cached or not.

boolean

Returns whether results of getSpanIndex method are cached or not.

void
setSpanGroupIndexCacheEnabled(boolean cacheSpanGroupIndices)

Sets whether the results of getSpanGroupIndex method should be cached or not.

void
setSpanIndexCacheEnabled(boolean cacheSpanIndices)

Sets whether the results of getSpanIndex method should be cached or not.

Constants

FILL_ALL_SPANS

Added in 1.1.0-alpha01
public static final int FILL_ALL_SPANS = -2

Value returned by getSpanSize: fill all spans. It's equivalent of returning the spanCount.

FILL_ALL_SPANS_AND_PADDINGS

Added in 1.1.0-alpha01
public static final int FILL_ALL_SPANS_AND_PADDINGS = -1

Value returned by getSpanSize: 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-alpha01
public LeanbackSpanSizeLookup()

Public methods

getDefault

Added in 1.1.0-alpha01
public static @NonNull LeanbackSpanSizeLookup getDefault()

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

getSpanGroupIndex

Added in 1.1.0-alpha01
public int getSpanGroupIndex(int adapterPosition, int spanCount)

Returns the index of the group this position belongs.

If VerticalGridView, this is a row value. If 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
int adapterPosition

The position in adapter

int spanCount

The total number of spans in the grid

Returns
int

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

getSpanIndex

Added in 1.1.0-alpha01
public int getSpanIndex(int position, int spanCount)

Returns the final span index of the provided position.

If VerticalGridView, this is a column value. If 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 (setSpanIndexCacheEnabled) 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. 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.1.0-alpha01
public abstract int getSpanSize(int position)

Returns the number of spans this item will take, or FILL_ALL_SPANS_AND_PADDINGS, FILL_ALL_SPANS.

Parameters
int position

The adapter position of the item.

Returns
int

The number of spans this item will take, or FILL_ALL_SPANS_AND_PADDINGS, FILL_ALL_SPANS.

getSpanSize

Added in 1.1.0-alpha01
public int getSpanSize(int position, int spanCount)

Returns the number of spans this item will take, or FILL_ALL_SPANS_AND_PADDINGS. Default implementation calls getSpanSize without spanCount. Subclass may override this method to return span size based on spanCount.

Parameters
int position

The adapter position of the item.

int spanCount

The total available spans of the grid.

Returns
int

The number of spans this item will take, or FILL_ALL_SPANS_AND_PADDINGS.

invalidateSpanGroupIndexCache

Added in 1.1.0-alpha01
public void invalidateSpanGroupIndexCache()

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

invalidateSpanIndexCache

Added in 1.1.0-alpha01
public void invalidateSpanIndexCache()

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

isSpanGroupIndexCacheEnabled

Added in 1.1.0-alpha01
public boolean isSpanGroupIndexCacheEnabled()

Returns whether results of getSpanGroupIndex method are cached or not.

Returns
boolean

True if results of getSpanGroupIndex are cached.

isSpanIndexCacheEnabled

Added in 1.1.0-alpha01
public boolean isSpanIndexCacheEnabled()

Returns whether results of getSpanIndex method are cached or not.

Returns
boolean

True if results of getSpanIndex are cached.

setSpanGroupIndexCacheEnabled

Added in 1.1.0-alpha01
public void setSpanGroupIndexCacheEnabled(boolean cacheSpanGroupIndices)

Sets whether the results of getSpanGroupIndex method should be cached or not. By default these values are not cached. If you are not overriding getSpanGroupIndex 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
boolean cacheSpanGroupIndices

Whether results of getGroupSpanIndex should be cached or not.

setSpanIndexCacheEnabled

Added in 1.1.0-alpha01
public void setSpanIndexCacheEnabled(boolean cacheSpanIndices)

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

Parameters
boolean cacheSpanIndices

Whether results of getSpanIndex should be cached or not.