androidx.tv.foundation.lazy.grid

Interfaces

TvGridCells

This class describes the count and the sizes of columns in vertical grids, or rows in horizontal grids.

TvLazyGridItemInfo

Contains useful information about an individual item in lazy grids like TvLazyVerticalGrid.

TvLazyGridItemScope

Receiver scope being used by the item content parameter of TvLazyVerticalGrid.

TvLazyGridItemSpanScope

Scope of lambdas used to calculate the spans of items in lazy grids.

TvLazyGridLayoutInfo

Contains useful information about the currently displayed layout state of lazy grids like TvLazyVerticalGrid.

TvLazyGridScope

Receiver scope which is used by TvLazyVerticalGrid.

Classes

TvGridCells.Adaptive

Defines a grid with as many rows or columns as possible on the condition that every cell has at least minSize space and all extra space distributed evenly.

TvGridCells.Fixed

Defines a grid with fixed number of rows or columns.

TvGridCells.FixedSize

Defines a grid with as many rows or columns as possible on the condition that every cell takes exactly size space.

TvGridItemSpan

Represents the span of an item in a TvLazyVerticalGrid.

TvLazyGridState

A state object that can be hoisted to control and observe scrolling.

Annotations

TvLazyGridScopeMarker

DSL marker used to distinguish between lazy grid dsl scope and the item content scope.

Top-level functions summary

TvGridItemSpan
TvGridItemSpan(currentLineSpan: Int)

Creates a TvGridItemSpan with a specified currentLineSpan.

Unit
@Composable
TvLazyHorizontalGrid(
    rows: TvGridCells,
    modifier: Modifier,
    state: TvLazyGridState,
    contentPadding: PaddingValues,
    reverseLayout: Boolean,
    horizontalArrangement: Arrangement.Horizontal,
    verticalArrangement: Arrangement.Vertical,
    userScrollEnabled: Boolean,
    pivotOffsets: PivotOffsets,
    content: TvLazyGridScope.() -> Unit
)

A lazy horizontal grid layout.

Unit
@Composable
TvLazyVerticalGrid(
    columns: TvGridCells,
    modifier: Modifier,
    state: TvLazyGridState,
    contentPadding: PaddingValues,
    reverseLayout: Boolean,
    verticalArrangement: Arrangement.Vertical,
    horizontalArrangement: Arrangement.Horizontal,
    userScrollEnabled: Boolean,
    pivotOffsets: PivotOffsets,
    content: TvLazyGridScope.() -> Unit
)

A lazy vertical grid layout.

TvLazyGridState
@Composable
rememberTvLazyGridState(
    initialFirstVisibleItemIndex: Int,
    initialFirstVisibleItemScrollOffset: Int
)

Creates a TvLazyGridState that is remembered across compositions.

Extension functions summary

inline Unit
<T : Any?> TvLazyGridScope.items(
    items: Array<T>,
    noinline key: ((item) -> Any)?,
    noinline span: (TvLazyGridItemSpanScope.(item) -> TvGridItemSpan)?,
    noinline contentType: (item) -> Any,
    crossinline itemContent: @Composable TvLazyGridItemScope.(item) -> Unit
)

Adds an array of items.

inline Unit
<T : Any?> TvLazyGridScope.items(
    items: List<T>,
    noinline key: ((item) -> Any)?,
    noinline span: (TvLazyGridItemSpanScope.(item) -> TvGridItemSpan)?,
    noinline contentType: (item) -> Any,
    crossinline itemContent: @Composable TvLazyGridItemScope.(item) -> Unit
)

Adds a list of items.

inline Unit
<T : Any?> TvLazyGridScope.itemsIndexed(
    items: Array<T>,
    noinline key: ((index: Int, item) -> Any)?,
    noinline span: (TvLazyGridItemSpanScope.(index: Int, item) -> TvGridItemSpan)?,
    crossinline contentType: (index: Int, item) -> Any,
    crossinline itemContent: @Composable TvLazyGridItemScope.(index: Int, item) -> Unit
)

Adds an array of items where the content of an item is aware of its index.

inline Unit
<T : Any?> TvLazyGridScope.itemsIndexed(
    items: List<T>,
    noinline key: ((index: Int, item) -> Any)?,
    noinline span: (TvLazyGridItemSpanScope.(index: Int, item) -> TvGridItemSpan)?,
    crossinline contentType: (index: Int, item) -> Any,
    crossinline itemContent: @Composable TvLazyGridItemScope.(index: Int, item) -> Unit
)

Adds a list of items where the content of an item is aware of its index.

Top-level functions

TvGridItemSpan

fun TvGridItemSpan(currentLineSpan: Int): TvGridItemSpan

Creates a TvGridItemSpan with a specified currentLineSpan. This will be the horizontal span for an item of a TvLazyVerticalGrid.

TvLazyHorizontalGrid

@Composable
fun TvLazyHorizontalGrid(
    rows: TvGridCells,
    modifier: Modifier = Modifier,
    state: TvLazyGridState = rememberTvLazyGridState(),
    contentPadding: PaddingValues = PaddingValues(0.dp),
    reverseLayout: Boolean = false,
    horizontalArrangement: Arrangement.Horizontal = if (!reverseLayout) Arrangement.Start else Arrangement.End,
    verticalArrangement: Arrangement.Vertical = Arrangement.Top,
    userScrollEnabled: Boolean = true,
    pivotOffsets: PivotOffsets = PivotOffsets(),
    content: TvLazyGridScope.() -> Unit
): Unit

A lazy horizontal grid layout. It composes only visible columns of the grid.

Parameters
rows: TvGridCells

a class describing how cells form rows, see TvGridCells doc for more information

modifier: Modifier = Modifier

the modifier to apply to this layout

state: TvLazyGridState = rememberTvLazyGridState()

the state object to be used to control or observe the list's state

contentPadding: PaddingValues = PaddingValues(0.dp)

specify a padding around the whole content

reverseLayout: Boolean = false

reverse the direction of scrolling and layout, when true items will be composed from the end to the start and TvLazyGridState.firstVisibleItemIndex == 0 will mean the first item is located at the end.

horizontalArrangement: Arrangement.Horizontal = if (!reverseLayout) Arrangement.Start else Arrangement.End

The horizontal arrangement of the layout's children

verticalArrangement: Arrangement.Vertical = Arrangement.Top

The vertical arrangement of the layout's children

userScrollEnabled: Boolean = true

whether the scrolling via the user gestures or accessibility actions is allowed. You can still scroll programmatically using the state even when it is disabled.

pivotOffsets: PivotOffsets = PivotOffsets()

offsets that are used when implementing Scrolling with Offset

pivotOffsets: PivotOffsets = PivotOffsets()

offsets of child element within the parent and starting edge of the child from the pivot defined by the parentOffset.

content: TvLazyGridScope.() -> Unit

the TvLazyGridScope which describes the content

TvLazyVerticalGrid

@Composable
fun TvLazyVerticalGrid(
    columns: TvGridCells,
    modifier: Modifier = Modifier,
    state: TvLazyGridState = rememberTvLazyGridState(),
    contentPadding: PaddingValues = PaddingValues(0.dp),
    reverseLayout: Boolean = false,
    verticalArrangement: Arrangement.Vertical = if (!reverseLayout) Arrangement.Top else Arrangement.Bottom,
    horizontalArrangement: Arrangement.Horizontal = Arrangement.Start,
    userScrollEnabled: Boolean = true,
    pivotOffsets: PivotOffsets = PivotOffsets(),
    content: TvLazyGridScope.() -> Unit
): Unit

A lazy vertical grid layout. It composes only visible rows of the grid.

Parameters
columns: TvGridCells

describes the count and the size of the grid's columns, see TvGridCells doc for more information

modifier: Modifier = Modifier

the modifier to apply to this layout

state: TvLazyGridState = rememberTvLazyGridState()

the state object to be used to control or observe the list's state

contentPadding: PaddingValues = PaddingValues(0.dp)

specify a padding around the whole content

reverseLayout: Boolean = false

reverse the direction of scrolling and layout. When true, items will be laid out in the reverse order and TvLazyGridState.firstVisibleItemIndex == 0 means that grid is scrolled to the bottom. Note that reverseLayout does not change the behavior of verticalArrangement, e.g. with Arrangement.Top (top) 123### (bottom) becomes (top) 321### (bottom).

verticalArrangement: Arrangement.Vertical = if (!reverseLayout) Arrangement.Top else Arrangement.Bottom

The vertical arrangement of the layout's children

horizontalArrangement: Arrangement.Horizontal = Arrangement.Start

The horizontal arrangement of the layout's children

userScrollEnabled: Boolean = true

whether the scrolling via the user gestures or accessibility actions is allowed. You can still scroll programmatically using the state even when it is disabled.

pivotOffsets: PivotOffsets = PivotOffsets()

offsets that are used when implementing Scrolling with Offset

pivotOffsets: PivotOffsets = PivotOffsets()

offsets of child element within the parent and starting edge of the child from the pivot defined by the parentOffset.

content: TvLazyGridScope.() -> Unit

the TvLazyGridScope which describes the content

rememberTvLazyGridState

@Composable
fun rememberTvLazyGridState(
    initialFirstVisibleItemIndex: Int = 0,
    initialFirstVisibleItemScrollOffset: Int = 0
): TvLazyGridState

Creates a TvLazyGridState that is remembered across compositions.

Changes to the provided initial values will not result in the state being recreated or changed in any way if it has already been created.

Parameters
initialFirstVisibleItemIndex: Int = 0

the initial value for TvLazyGridState.firstVisibleItemIndex

initialFirstVisibleItemScrollOffset: Int = 0

the initial value for TvLazyGridState.firstVisibleItemScrollOffset

Extension functions

inline fun <T : Any?> TvLazyGridScope.items(
    items: Array<T>,
    noinline key: ((item) -> Any)? = null,
    noinline span: (TvLazyGridItemSpanScope.(item) -> TvGridItemSpan)? = null,
    noinline contentType: (item) -> Any = { null },
    crossinline itemContent: @Composable TvLazyGridItemScope.(item) -> Unit
): Unit

Adds an array of items.

Parameters
items: Array<T>

the data array

noinline key: ((item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.

noinline span: (TvLazyGridItemSpanScope.(item) -> TvGridItemSpan)? = null

define custom spans for the items. Default is 1x1. It is good practice to leave it null when this matches the intended behavior, as providing a custom implementation impacts performance

noinline contentType: (item) -> Any = { null }

a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.

crossinline itemContent: @Composable TvLazyGridItemScope.(item) -> Unit

the content displayed by a single item

inline fun <T : Any?> TvLazyGridScope.items(
    items: List<T>,
    noinline key: ((item) -> Any)? = null,
    noinline span: (TvLazyGridItemSpanScope.(item) -> TvGridItemSpan)? = null,
    noinline contentType: (item) -> Any = { null },
    crossinline itemContent: @Composable TvLazyGridItemScope.(item) -> Unit
): Unit

Adds a list of items.

Parameters
items: List<T>

the data list

noinline key: ((item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.

noinline span: (TvLazyGridItemSpanScope.(item) -> TvGridItemSpan)? = null

define custom spans for the items. Default is 1x1. It is good practice to leave it null when this matches the intended behavior, as providing a custom implementation impacts performance

noinline contentType: (item) -> Any = { null }

a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.

crossinline itemContent: @Composable TvLazyGridItemScope.(item) -> Unit

the content displayed by a single item

itemsIndexed

inline fun <T : Any?> TvLazyGridScope.itemsIndexed(
    items: Array<T>,
    noinline key: ((index: Int, item) -> Any)? = null,
    noinline span: (TvLazyGridItemSpanScope.(index: Int, item) -> TvGridItemSpan)? = null,
    crossinline contentType: (index: Int, item) -> Any = { _, _ -> null },
    crossinline itemContent: @Composable TvLazyGridItemScope.(index: Int, item) -> Unit
): Unit

Adds an array of items where the content of an item is aware of its index.

Parameters
items: Array<T>

the data array

noinline key: ((index: Int, item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.

noinline span: (TvLazyGridItemSpanScope.(index: Int, item) -> TvGridItemSpan)? = null

define custom spans for the items. Default is 1x1. It is good practice to leave it null when this matches the intended behavior, as providing a custom implementation impacts performance

crossinline contentType: (index: Int, item) -> Any = { _, _ -> null }

a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.

crossinline itemContent: @Composable TvLazyGridItemScope.(index: Int, item) -> Unit

the content displayed by a single item

itemsIndexed

inline fun <T : Any?> TvLazyGridScope.itemsIndexed(
    items: List<T>,
    noinline key: ((index: Int, item) -> Any)? = null,
    noinline span: (TvLazyGridItemSpanScope.(index: Int, item) -> TvGridItemSpan)? = null,
    crossinline contentType: (index: Int, item) -> Any = { _, _ -> null },
    crossinline itemContent: @Composable TvLazyGridItemScope.(index: Int, item) -> Unit
): Unit

Adds a list of items where the content of an item is aware of its index.

Parameters
items: List<T>

the data list

noinline key: ((index: Int, item) -> Any)? = null

a factory of stable and unique keys representing the item. Using the same key for multiple items in the grid is not allowed. Type of the key should be saveable via Bundle on Android. If null is passed the position in the grid will represent the key. When you specify the key the scroll position will be maintained based on the key, which means if you add/remove items before the current visible item the item with the given key will be kept as the first visible one.

noinline span: (TvLazyGridItemSpanScope.(index: Int, item) -> TvGridItemSpan)? = null

define custom spans for the items. Default is 1x1. It is good practice to leave it null when this matches the intended behavior, as providing a custom implementation impacts performance

crossinline contentType: (index: Int, item) -> Any = { _, _ -> null }

a factory of the content types for the item. The item compositions of the same type could be reused more efficiently. Note that null is a valid type and items of such type will be considered compatible.

crossinline itemContent: @Composable TvLazyGridItemScope.(index: Int, item) -> Unit

the content displayed by a single item