FlatList
本页面由 PageTurner AI 翻译(测试版)。未经项目官方认可。 发现错误? 报告问题 →
一个高性能的基础平面列表渲染接口,支持以下实用功能:
-
完全跨平台
-
可选的横向模式
-
可配置的可见性回调
-
支持头部组件
-
支持尾部组件
-
支持分隔符
-
下拉刷新
-
滚动加载
-
支持滚动到索引位置
-
多列布局支持
如需分组支持,请使用 <SectionList>
示例
- TypeScript
- JavaScript
要渲染多列布局,请使用 numColumns 属性。相比 flexWrap 布局,这种方法可以避免与项目高度逻辑的冲突
下方提供更复杂的选择功能示例
-
通过向
FlatList传递extraData={selectedId},确保FlatList本身会在状态变化时重新渲染。若不设置此 prop,FlatList作为PureComponent无法感知属性变化,不会重新渲染项目 -
keyExtractor告知列表使用项目的id作为 React key,而非默认的key属性
- TypeScript
- JavaScript
这是对 <VirtualizedList> 的便捷封装,因此会继承其属性(以及 <ScrollView> 的属性),此处未明确列出的属性也会被继承,同时需要注意以下事项:
-
当内容滚动出渲染窗口时,内部状态不会被保留。请确保所有数据都存储在项数据或外部状态库(如 Flux/Redux/Relay)中
-
这是
PureComponent,意味着如果props保持浅层相等(shallow-equal),则不会重新渲染。请确保renderItem函数依赖的所有内容都通过属性传递(如extraData),且该属性在更新后不是===相等的,否则界面可能不会更新。这包括data属性以及父组件状态 -
为控制内存占用并确保滚动流畅,列表内容会在屏幕外异步渲染。这意味着用户可能快速滚动时暂时看到空白区域,这是为平衡性能所做的设计取舍。我们正在持续优化该体验。
-
默认情况下,列表会查找每个项目的
key属性作为 React key。您也可以通过keyExtractor属性提供自定义键
参考
属性
VirtualizedList 属性
Required renderItem
renderItem({
item: ItemT,
index: number,
separators: {
highlight: () => void;
unhighlight: () => void;
updateProps: (select: 'leading' | 'trailing', newProps: any) => void;
}
}): JSX.Element;
接收 data 中的项目并渲染到列表中
提供额外元数据(如 index)和通用函数 separators.updateProps。当常用方法 highlight/unhighlight(设置 highlighted: boolean 属性)无法满足需求时,可通过此函数修改前导/尾部分隔符的任意渲染属性
| Type |
|---|
| function |
-
item(对象):待渲染的data中的项目 -
index(数字):该项目在data数组中的索引位置 -
separators(Object)highlight(Function)unhighlight(Function)updateProps(Function)select(enum('leading', 'trailing'))newProps(Object)
使用示例:
<FlatList
ItemSeparatorComponent={
Platform.OS !== 'android' &&
(({highlighted}) => (
<View
style={[style.separator, highlighted && {marginLeft: 0}]}
/>
))
}
data={[{title: 'Title Text', key: 'item1'}]}
renderItem={({item, index, separators}) => (
<TouchableHighlight
key={item.key}
onPress={() => this._onPress(item)}
onShowUnderlay={separators.highlight}
onHideUnderlay={separators.unhighlight}>
<View style={{backgroundColor: 'white'}}>
<Text>{item.title}</Text>
</View>
</TouchableHighlight>
)}
/>
Required data
要渲染的数据数组(或类数组结构)。如需使用其他数据类型,请直接使用 VirtualizedList。
| Type |
|---|
| ArrayLike |
ItemSeparatorComponent
渲染在列表项之间(不包括顶部和底部)。默认提供 highlighted 和 leadingItem 属性。renderItem 提供 separators.highlight/unhighlight 来更新 highlighted 属性,也可通过 separators.updateProps 添加自定义属性。可以是 React 组件(如 SomeComponent)或 React 元素(如 <SomeComponent />)
| Type |
|---|
| component, function, element |
ListEmptyComponent
列表为空时渲染。可以是 React 组件(如 SomeComponent)或 React 元素(如 <SomeComponent />)
| Type |
|---|
| component, element |
ListFooterComponent
在所有列表项底部渲染。可以是 React 类组件(如 SomeComponent),也可以是 React 元素(如 <SomeComponent />)。
| Type |
|---|
| component, element |
ListFooterComponentStyle
用于 ListFooterComponent 内部 View 组件的样式。
| Type |
|---|
| View Style |
ListHeaderComponent
在所有列表项顶部渲染。可以是 React 类组件(如 SomeComponent),也可以是 React 元素(如 <SomeComponent />)。
| Type |
|---|
| component, element |
ListHeaderComponentStyle
用于 ListHeaderComponent 内部 View 组件的样式。
| Type |
|---|
| View Style |
columnWrapperStyle
当 numColumns > 1 时,用于多列布局容器的可选自定义样式。
| Type |
|---|
| View Style |
extraData
用于通知列表重新渲染的标记属性(因列表继承自 PureComponent)。若 renderItem、Header、Footer 等函数依赖 data 属性之外的任何数据,请将其置于此处并视为不可变数据。
| Type |
|---|
| any |
getItemLayout
(data, index) => {length: number, offset: number, index: number}
getItemLayout 是一个可选的性能优化项,它允许在提前知道项目尺寸(高度或宽度)时跳过动态内容的测量。getItemLayout 在项目尺寸固定的情况下非常高效,例如:
getItemLayout={(data, index) => (
{length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index}
)}
添加getItemLayout能显著提升数百项列表的性能。若指定了ItemSeparatorComponent,计算偏移量时需包含分隔符尺寸(高度/宽度)。
| Type |
|---|
| function |
horizontal
若为 true,则水平排列项目而非垂直堆叠。
| Type |
|---|
| boolean |
initialNumToRender
初始批次渲染的项目数量。应足以填满屏幕但不宜过多。注意:为提升滚动至顶部的感知性能,这些初始项目永远不会因窗口化渲染而被卸载。
| Type | Default |
|---|---|
| number | 10 |
initialScrollIndex
不从首项开始渲染,而是从 initialScrollIndex 指定位置开始。此设置会禁用"滚动至顶部"优化(该优化会永久渲染前 initialNumToRender 个项目),并立即渲染从该初始索引开始的项。需要实现 getItemLayout。
| Type |
|---|
| number |
inverted
反转滚动方向(通过 -1 的缩放变换实现)。
| Type |
|---|
| boolean |
keyExtractor
(item: ItemT, index: number) => string;
用于为指定索引处的项目生成唯一键。该键用于缓存及作为 React key 跟踪项目重排序。默认提取器依次检查 item.key、item.id,最后回退使用索引(与 React 行为一致)。
| Type |
|---|
| function |
numColumns
仅当 horizontal={false} 时支持多列渲染,布局呈 Z 字形排列(类似 flexWrap)。所有条目应保持相同高度——不支持瀑布流布局。
| Type |
|---|
| number |
onRefresh
() => void;
若提供此回调,将添加标准 RefreshControl 组件实现“下拉刷新”功能。请确保同时正确设置 refreshing 属性。
| Type |
|---|
| function |
onViewableItemsChanged
当行项目的可见性发生变化时触发(可见性规则由 viewabilityConfig 属性定义)。
progressViewOffset
当加载指示器需要偏移量才能正确显示时设置此属性。
| Type |
|---|
| number |
refreshing
在等待刷新数据时将此属性设为 true。
| Type |
|---|
| boolean |
removeClippedSubviews
可提升长列表滚动性能。Android 平台默认值为 true。
注意:某些情况下可能导致内容异常(如内容缺失)—— 请自行承担使用风险。
| Type |
|---|
| boolean |
viewabilityConfig
类型定义及完整文档请参见 ViewabilityHelper.js。
| Type |
|---|
| ViewabilityConfig |
viewabilityConfig 接收类型为 ViewabilityConfig 的对象,包含以下属性:
| Property | Type |
|---|---|
| minimumViewTime | number |
| viewAreaCoveragePercentThreshold | number |
| itemVisiblePercentThreshold | number |
| waitForInteraction | boolean |
必须设置 viewAreaCoveragePercentThreshold 或 itemVisiblePercentThreshold 中的至少一项。为避免此错误,请在 constructor 中完成配置:
Error: Changing viewabilityConfig on the fly is not supported
constructor (props) {
super(props)
this.viewabilityConfig = {
waitForInteraction: true,
viewAreaCoveragePercentThreshold: 95
}
}
<FlatList
viewabilityConfig={this.viewabilityConfig}
...
minimumViewTime
条目在物理可见区域停留的最短时间(毫秒),满足此条件才会触发可见性回调。设置较高值可避免快速滚动时误判可见性。
viewAreaCoveragePercentThreshold
部分可见条目被判定为“可见”时需覆盖视口的百分比(0-100)。完全可见条目始终视为可见。设为 0 表示条目在视口中露出一个像素即视为可见;设为 100 表示条目必须完全可见或覆盖整个视口才视为可见。
itemVisiblePercentThreshold
与 viewAreaCoveragePercentThreshold 类似,但以条目自身的可见比例(而非覆盖视口的比例)作为判定标准。
waitForInteraction
在用户滚动或调用 recordInteraction 之前,所有条目均不会被视为可见。
viewabilityConfigCallbackPairs
由 ViewabilityConfig 和 onViewableItemsChanged 组成的配置对数组。当满足特定 ViewabilityConfig 的条件时,其对应的 onViewableItemsChanged 回调将被触发。详见 ViewabilityHelper.js 中的 flow 类型定义和文档说明。
| Type |
|---|
| array of ViewabilityConfigCallbackPair |
方法
flashScrollIndicators()
flashScrollIndicators();
暂时显示滚动指示器。
getNativeScrollRef()
getNativeScrollRef(): React.ElementRef<typeof ScrollViewComponent>;
获取底层滚动组件的引用。
getScrollResponder()
getScrollResponder(): ScrollResponderMixin;
获取底层滚动响应器句柄。
getScrollableNode()
getScrollableNode(): any;
获取底层可滚动节点句柄。
scrollToEnd()
scrollToEnd(params?: {animated?: boolean});
滚动至内容末尾。未提供 getItemLayout 属性时可能出现卡顿。
参数:
| Name | Type |
|---|---|
| params | object |
有效的 params 键如下:
- 'animated' (boolean) - 是否启用滚动动画。默认为
true。
scrollToIndex()
scrollToIndex: (params: {
index: number;
animated?: boolean;
viewOffset?: number;
viewPosition?: number;
});
滚动至指定索引项,并根据viewPosition参数定位:0置顶,1置底,0.5居中。
注意:未指定
getItemLayout属性时无法滚动至渲染窗口外的位置。
参数:
| Name | Type |
|---|---|
| params Required | object |
有效的 params 键如下:
-
'animated' (boolean) - 是否启用滚动动画。默认为
true。 -
'index' (number) - 目标索引(必填)。
-
'viewOffset' (number) - 最终目标位置的固定像素偏移量。
-
'viewPosition' (number) - 目标位置:
0(顶部)、1(底部)或0.5(居中)。
scrollToItem()
scrollToItem(params: {
animated?: ?boolean,
item: Item,
viewPosition?: number,
});
需线性遍历数据——建议优先使用scrollToIndex。
注意:未指定
getItemLayout属性时无法滚动至渲染窗口外的位置。
参数:
| Name | Type |
|---|---|
| params Required | object |
有效的 params 键如下:
-
'animated' (boolean) - 是否启用滚动动画。默认为
true。 -
'item' (object) - 目标数据项(必填)。
-
'viewPosition' (number)
scrollToOffset()
scrollToOffset(params: {
offset: number;
animated?: boolean;
});
滚动到列表内指定的内容像素偏移位置
参数:
| Name | Type |
|---|---|
| params Required | object |
有效的 params 键如下:
-
'offset' (number) - 目标偏移量(当
horizontal为 true 时表示 x 值,其他情况表示 y 值)(必填)。 -
'animated' (boolean) - 是否启用滚动动画。默认为
true。