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
Recommended version:
~58.0.15

Expo UI 的 LazyRow 与官方 Jetpack Compose LazyRow API 一致,显示一个横向滚动的列表。

🌐 Expo UI LazyRow matches the official Jetpack Compose LazyRow API and displays a horizontally scrolling list.

LazyRow rendering colored category cards in a horizontally scrolling listLazyRow rendering colored category cards in a horizontally scrolling list

安装

🌐 Installation

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.

用法

🌐 Usage

基础懒惰行

🌐 Basic lazy row

[LazyRow](#lazyrow) 的每个子元素都是一个条目。使用 horizontalArrangement 属性来调整条目之间的间距,使用 contentPadding 属性来给列表内容添加内边距。

🌐 Each child of LazyRow is one item. Use the horizontalArrangement prop to space the items and the contentPadding prop to add padding around the list content.

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> ); }

大型列表

🌐 Large lists

使用 LazyRow.Items 来渲染数组中的项目。把项目作为 data 传入,每个项目用 keyExtractor 提供唯一的 key,并提供一个 children 函数来返回项目的视图。LazyRow.Items 只渲染靠近可见区域的项目,并在你滚动时重复使用它们。

🌐 Use LazyRow.Items to render items from an array. Pass the items as data, a unique key for each item with keyExtractor, and a children function that returns the view for an item. LazyRow.Items renders only the items near the visible area and reuses them while you scroll.

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> ); }

在展示物品时遵守这些规则:

🌐 Follow these rules when you render items:

  • 给 LazyRow 设置固定高度。还没准备好的项目显示一个空占位符,填满整行的高度。
  • 在组件外定义 children 函数,或者把它包在 useCallback 里。否则,每次组件渲染时,可见区域附近的项都会重新渲染。
  • 一个重用的视图会保留它之前显示的项目的本地状态(useState)。当项目改变时重置状态,或者把状态保存在视图外部。
  • 将 estimatedItemSize 设置为接近你物品宽度的数值,单位是独立像素(dp)。
  • 使用 overscanCount 来在可见项目的前后准备更多项目。
  • 要更新项目,传入一个新的 data 数组。不要在原地修改数组。
  • 把 recycling={false} 设置成提前渲染每一项。

应用接口

🌐 API

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

No API data file found, sorry!