Reference version

This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

LazyRow

用于显示水平滚动列表的 Jetpack Compose LazyRow 组件。

Android
Included in Expo Go

Expo UI LazyRow 与官方 Jetpack Compose LazyRow API 一致,并显示水平滚动列表。

LazyRow 在水平滚动列表中渲染彩色分类卡片LazyRow 在水平滚动列表中渲染彩色分类卡片

安装

Terminal
- npx expo install @expo/ui
- yarn expo install @expo/ui
- pnpm expo install @expo/ui
- bun expo install @expo/ui

If you are installing this in an existing React Native app, make sure to install expo in your project.

用法

基础懒加载行

LazyRow 的每个子项都是一个项目。使用 horizontalArrangement 属性设置项目之间的间距,使用 contentPadding 属性为列表内容添加内边距。

BasicLazyRow.tsx
import { Host, LazyRow, Text, useMaterialColors, } from '@expo/ui/jetpack-compose'; import { border, padding, } from '@expo/ui/jetpack-compose/modifiers'; export default function BasicLazyRow() { const colors = useMaterialColors(); return ( <Host style={{ height: 100 }}> <LazyRow horizontalArrangement={{ spacedBy: 8 }} verticalAlignment="center"> <Text style={{ fontSize: 28 }} color={colors.onBackground} modifiers={[ border(1, colors.outline), padding(12, 6, 12, 6), ]}> Photos </Text> <Text style={{ fontSize: 28 }} color={colors.onBackground} modifiers={[ border(1, colors.outline), padding(12, 6, 12, 6), ]}> Music </Text> <Text style={{ fontSize: 28 }} color={colors.onBackground} modifiers={[ border(1, colors.outline), padding(12, 6, 12, 6), ]}> Videos </Text> </LazyRow> </Host> ); }

大型列表

使用 LazyRow.Items 从数组渲染项目。将项目作为 data 传入,使用 keyExtractor 为每个项目提供唯一键,并传入一个返回项目视图的 children 函数。LazyRow.Items 只渲染可见区域附近的项目,并在滚动时复用这些项目。

LargeLazyRow.tsx
import { Host, LazyRow, Text, useMaterialColors, } from '@expo/ui/jetpack-compose'; import { border, padding, } from '@expo/ui/jetpack-compose/modifiers'; const items = Array.from( { length: 10000 }, (_, i) => `Item ${i + 1}` ); function Chip({ label }: { label: string }) { const colors = useMaterialColors(); return ( <Text style={{ fontSize: 28 }} color={colors.onBackground} modifiers={[ border(1, colors.outline), padding(12, 6, 12, 6), ]}> {label} </Text> ); } function renderItem({ item }: { item: string }) { return <Chip label={item} />; } export default function LargeLazyRow() { return ( <Host style={{ height: 100 }}> <LazyRow horizontalArrangement={{ spacedBy: 8 }} verticalAlignment="center"> <LazyRow.Items data={items} keyExtractor={item => item} estimatedItemSize={120}> {renderItem} </LazyRow.Items> </LazyRow> </Host> ); }

渲染项目时请遵循以下规则:

  • 为 LazyRow 设置固定高度。尚未准备好的项目会显示填满行高的空占位符。
  • 在组件外定义 children 函数,或使用 useCallback 包装它。否则,每次组件渲染时,可见区域附近的项目都会重新渲染。
  • 复用的视图会保留之前显示项目的本地状态(useState)。项目变化时重置状态,或将状态保存在视图外部。
  • 将 estimatedItemSize 设置为接近项目宽度的值,单位为密度无关像素(dp)。
  • 使用 overscanCount 在可见项目前后预先准备更多项目。
  • 要更新项目,请传入新的 data 数组。不要原地修改数组。
  • 设置 recycling={false} 可预先渲染每个项目。

API

import { LazyRow } from '@expo/ui/jetpack-compose';

Components

LazyItems

Android

Type: React.Element<LazyItemsProps<T>>

A block of recycled rows inside LazyColumn or LazyRow, mirroring the Compose items(count, key) builder. Only a small pool of rows around the visible range is mounted, so large data sets stay cheap. Rows that are not ready yet show a placeholder of estimatedItemSize, or of the size last measured for that item.

Mount it as a direct child of LazyColumn or LazyRow.

LazyItemsProps

children

Android
Type: (info: { index: number, item: T }) => ReactElement

Renders an item. Wrap it in useCallback, or every item re-renders on each parent render. Recycled views are reused for other items, so their local state (useState) carries over. Reset it when the item changes, or keep the state outside the view.

data

Android
Type: readonly T[]

Items to display. Replace the array when updating data.

estimatedItemSize

Android
Optional • Type: number • Default: 64

Placeholder size in dp along the scroll axis, until an item is measured. Must be positive. Ignored when recycling is false.

keyExtractor

Android
Type: (item: T, index: number) => string

Returns a stable, unique string key, also used as the lazy list item key.

overscanCount

Android
Optional • Type: number • Default: 10

Extra items to prepare on each side of the visible items. Must be a non-negative integer. Ignored when recycling is false.

recycling

Android
Optional • Type: boolean • Default: true

Renders only the items near the visible range and reuses them while scrolling. Set to false to render every item at once. Set it once; changing it remounts the items.

LazyRow

Android

Type: React.Element<LazyRowProps>

A lazy row component that efficiently displays a horizontally scrolling list.

LazyRowProps

children

Android
Optional • Type: ReactNode

The content to display inside the lazy row.

contentPadding

Android
Optional • Type: ContentPadding

Content padding in dp.

horizontalArrangement

Android
Optional • Literal type: union

The horizontal arrangement of items. Can be a preset string or an object with spacedBy to specify spacing in dp.

Acceptable values are: 'center' | 'start' | 'end' | 'spaceBetween' | 'spaceAround' | 'spaceEvenly' | { spacedBy: number }

modifiers

Android
Optional • Type: ModifierConfig[]

Modifiers for the component.

verticalAlignment

Android
Optional • Literal type: string

The vertical alignment of items.

Acceptable values are: 'center' | 'top' | 'bottom'