【React Native】文件翻譯閱讀紀錄 - Components(組件) - FlatList
![]() |
| Facebook Open Source React Native |
FlatList
一個高性能的界面,用於渲染簡單,扁平的列表,支持最方便的功能:
- 完全跨平台。
- 可選水平模式。
- 可配置的可見度回調。
- 標題支持。
- 頁腳支持。
- 分隔支持。
- 拉到刷新。
- 滾動加載。
- ScrollToIndex支持。
如果需要節支持,請使用
<SectionList>.
最小例子:
<FlatList
data={[{key: 'a'}, {key: 'b'}]}
renderItem={({item}) => <Text>{item.key}</Text>}
/>
更複雜的多選示例演示了PureComponent用於perf優化和避免錯誤的用法。
- 通過綁定onPressItem處理程序,props將保持===並且PureComponent將防止浪費的重新呈現,除非實際的id,selected或title props改變,即使在MyListItem中呈現的組件沒有這樣的優化。
- 通過將extraData = {this.state}傳遞給FlatList,我們確保當state.selected更改時,FlatList本身將重新呈現。如果不設置此prop,FlatList將不知道它需要重新渲染任何項目,因為它也是PureComponent,並且prop比較不會顯示任何更改。
- keyExtractor告訴列表使用反應鍵的id而不是默認的鍵屬性。
class MyListItem extends React.PureComponent {
_onPress = () => {
this.props.onPressItem(this.props.id);
};
render() {
const textColor = this.props.selected ? "red" : "black";
return (
<TouchableOpacity onPress={this._onPress}>
<View>
<Text style={{ color: textColor }}>
{this.props.title}
</Text>
</View>
</TouchableOpacity>
);
}
}
class MultiSelectList extends React.PureComponent {
state = {selected: (new Map(): Map<string, boolean>)};
_keyExtractor = (item, index) => item.id;
_onPressItem = (id: string) => {
// updater functions are preferred for transactional updates
this.setState((state) => {
// copy the map rather than modifying state.
const selected = new Map(state.selected);
selected.set(id, !selected.get(id)); // toggle
return {selected};
});
};
_renderItem = ({item}) => (
<MyListItem
id={item.id}
onPressItem={this._onPressItem}
selected={!!this.state.selected.get(item.id)}
title={item.title}
/>
);
render() {
return (
<FlatList
data={this.props.data}
extraData={this.state}
keyExtractor={this._keyExtractor}
renderItem={this._renderItem}
/>
);
}
}
這是<VirtualizedList>的一個便利包裝器,因此繼承了這裡沒有明確列出的道具(以及<ScrollView>的道具),以及以下警告:
- 當內容滾出渲染窗口時,不會保留內部狀態。確保在項目數據或外部存儲(如Flux,Redux或Relay)中捕獲所有數據。
- 這是一個PureComponent,這意味著如果道具保持淺層相等,它將不會重新渲染。確保renderItem函數所依賴的所有內容在更新後作為prop(例如extraData)傳遞,而不是===,否則您的UI可能無法更新更新。這包括數據prop和父組件狀態。
- 為了限制內存並啟用平滑滾動,內容在屏幕外異步呈現。這意味著可以比填充率更快地滾動並暫時看到空白內容。這是一個可以調整以適應每個應用程序需求的權衡,我們正在努力在幕後進行改進。
- 默認情況下,列表在每個項目上查找關鍵支柱,並將其用於React鍵。或者,您可以提供自定義keyExtractor道具。
還繼承ScrollView Props,除非它嵌套在同一方向的另一個FlatList中。
Props
ScrollViewprops...VirtualizedListprops...renderItemdataItemSeparatorComponentListEmptyComponentListFooterComponentListHeaderComponentcolumnWrapperStyleextraDatagetItemLayouthorizontalinitialNumToRenderinitialScrollIndexinvertedkeyExtractornumColumnsonEndReachedonEndReachedThresholdonRefreshonViewableItemsChangedprogressViewOffsetlegacyImplementationrefreshingremoveClippedSubviewsviewabilityConfigviewabilityConfigCallbackPairs
方法
Reference
Props
renderItem
renderItem({ item: Object, index: number, separators: { highlight: Function, unhighlight: Function, updateProps: Function(select: string, newProps: Object) } }) => ?React.Element
從數據中獲取項目並將其呈現到列表中。
如果需要,提供額外的元數據,如索引,以及更通用的separators.updateProps函數,它允許您設置要更改前導分隔符或尾隨分隔符的渲染的任何道具,以防更常見的突出顯示和不突出顯示(設置突出顯示的:布爾道具不足以滿足您的使用案例。
如果需要,提供額外的元數據,如索引,以及更通用的separators.updateProps函數,它允許您設置要更改前導分隔符或尾隨分隔符的渲染的任何道具,以防更常見的突出顯示和不突出顯示(設置突出顯示的:布爾道具不足以滿足您的使用案例。
| TYPE | 必需 |
|---|---|
| function | Yes |
Example usage:
<FlatList
ItemSeparatorComponent={Platform.OS !== 'android' && ({highlighted}) => (
<View style={[style.separator, highlighted && {marginLeft: 0}]} />
)}
data={[{title: 'Title Text', key: 'item1'}]}
renderItem={({item, separators}) => (
<TouchableHighlight
onPress={() => this._onPress(item)}
onShowUnderlay={separators.highlight}
onHideUnderlay={separators.unhighlight}>
<View style={{backgroundColor: 'white'}}>
<Text>{item.title}</Text>
</View>
</TouchableHighlight>
)}
/>
data
為簡單起見,數據只是一個普通的數組。如果要使用其他內容(如不可變列表),請直接使用基礎VirtualizedList。
| 類型 | 必需 |
|---|---|
| array | Yes |
ItemSeparatorComponent
在每個項目之間渲染,但不在頂部或底部。默認情況下,提供highlight和leadingItem道具。 renderItem提供separators.highlight / unhighlight,它將更新突出顯示的prop,但您也可以使用separators.updateProps添加自定義道具。
| 類型 | 必需 |
|---|---|
| component | No |
ListEmptyComponent
列表為空時呈現。可以是React Component Class,渲染函數或渲染元素。
| 類型 | 必需 |
|---|---|
| component, function, element | No |
ListFooterComponent
在所有項目的底部呈現。可以是React Component Class,渲染函數或渲染元素。
| 類型 | 必需 |
|---|---|
| component, function, element | No |
ListHeaderComponent
呈現在所有項目的頂部。可以是React Component Class,渲染函數或渲染元素。
| 類型 | 必需 |
|---|---|
| component, function, element | No |
columnWrapperStyle
numColumns> 1時生成的多項行的可選自定義樣式。
| 類型 | 必需 |
|---|---|
| style object | No |
extraData
用於告知列表重新呈現的標記屬性(因為它實現了PureComponent)。如果你的任何renderItem,Header,Footer等函數依賴於數據道具之外的任何東西,請將其粘貼在此處並進行不可變處理。
| 類型 | 必需 |
|---|---|
| any | No |
getItemLayout
(data, index) => {length: number, offset: number, index: number}
getItemLayout是一個可選的優化,如果您提前了解項目的高度,我們可以跳過動態內容的測量。如果你有固定的高度項,getItemLayout既高效又易於使用,例如:
getItemLayout={(data, index) => (
{length: ITEM_HEIGHT, offset: ITEM_HEIGHT * index, index}
)}
添加getItemLayout可以為幾百個項目的列表提供很好的性能提升。如果指定ItemSeparatorComponent,請記住在偏移計算中包括分隔符長度(高度或寬度)。
| 類型 | 必需 |
|---|---|
| function | No |
horizontal
如果為true,則水平呈現彼此相鄰的項目,而不是垂直堆疊。
| 類型 | 必需 |
|---|---|
| boolean | No |
initialNumToRender
初始批次中要渲染的項目數。這應該足以填滿屏幕,但不是更多。請注意,這些項目永遠不會作為窗口渲染的一部分卸載,以提高滾動到頂部操作的感知性能。
| 類型 | 必需 |
|---|---|
| number | No |
initialScrollIndex
而不是從第一個項目的頂部開始,從initialScrollIndex開始。這將禁用“滾動到頂部”優化,該優化使第一個initialNumToRender項始終呈現,並立即呈現從此初始索引開始的項。需要實現getItemLayout。
| 類型 | 必需 |
|---|---|
| number | No |
inverted
反轉滾動方向。使用-1的比例變換。
| 類型 | 必需 |
|---|---|
| boolean | No |
keyExtractor
(item: object, index: number) => string;
用於為指定索引處的給定項提取唯一鍵。密鑰用於緩存,並作為跟踪項目重新排序的反應鍵。默認提取器檢查item.key,然後回退到使用索引,就像React那樣。
| 類型 | 必需 |
|---|---|
| function | No |
numColumns
多列只能使用horizontal = {false}進行渲染,並且像flexWrap佈局一樣鋸齒形。項目應該都是相同的高度 - 不支持砌體佈局。
| 類型 | 必需 |
|---|---|
| number | No |
onEndReached
(info: {distanceFromEnd: number}) => void
當滾動位置在渲染內容的onEndReachedThreshold內時調用一次。
| 類型 | 必需 |
|---|---|
| function | No |
onEndReachedThreshold
距離末尾多少(以列表的可見長度為單位)列表的下邊緣必須來自內容的末尾才能觸發onEndReached回調。因此,當內容的結尾在列表的可見長度的一半之內時,值0.5將觸發onEndReached。
| 類型 | 必需 |
|---|---|
| number | No |
onRefresh
() => void
如果提供,將為“Pull to Refresh”功能添加標準RefreshControl。確保正確設置刷新道具。
| 類型 | 必需 |
|---|---|
| function | No |
onViewableItemsChanged
(info: {
viewableItems: array,
changed: array,
}) => void
當viewabilityConfig prop定義時,行的可見度發生變化時調用。
| 類型 | 必需 |
|---|---|
| function | No |
progressViewOffset
當需要偏移以使加載指示器正確顯示時設置此項。
| 類型 | 必需 | 平台 |
|---|---|---|
| number | No | Android |
legacyImplementation
可能沒有完整的功能奇偶校驗,用於調試和性能比較。
| 類型 | 必需 |
|---|---|
| boolean | No |
refreshing
在等待刷新的新數據時設置為true。
| 類型 | 必需 |
|---|---|
| boolean | No |
removeClippedSubviews
這可以改善大型列表的滾動性能。
注意:在某些情況下可能存在錯誤(缺少內容) - 使用風險自負。
| 類型 | 必需 |
|---|---|
| boolean | No |
viewabilityConfig
有關流類型和更多文檔,請參閱 ViewabilityHelper.js。
| 類型 | 必需 |
|---|---|
| ViewabilityConfig | No |
viewabilityConfig 採用 ViewabilityConfig 類型,該對象具有以下屬性
| PROPERTY | 必需 | 類型 |
|---|---|---|
| minimumViewTime | No | number |
| viewAreaCoveragePercentThreshold | No | number |
| itemVisiblePercentThreshold | No | number |
| waitForInteraction | No | boolean |
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
與viewAreaPercentThreshold類似,但考慮可見項目的百分比,而不是它所覆蓋的可查看區域的分數。
waitForInteraction
在用戶滾動或在渲染後調用recordInteraction之前,不會將任何內容視為可見。
viewabilityConfigCallbackPairs
ViewabilityConfig / onViewableItemsChanged對列表。當滿足相應的ViewabilityConfig條件時,將調用特定的onViewableItemsChanged。有關流類型和更多文檔,請參閱ViewabilityHelper.js。
| 類型 | 必需 |
|---|---|
| array of ViewabilityConfigCallbackPair | No |
方法
scrollToEnd()
scrollToEnd([params]);
滾動到內容的末尾。如果沒有 getItemLayout 道具,可能會很笨拙。
參數:
| 名稱 | 類型 | 必需 | 說明 |
|---|---|---|---|
| params | object | No | See below. |
有效的 params 鍵是:
- 'animated'(boolean) - 列表是否應該在滾動時執行動畫。默認為true。
scrollToIndex()
scrollToIndex(params);
滾動到指定索引處的項目,使其位於可查看區域中,以便viewPosition 0將其置於頂部,1位於底部,0.5位於中間。
注意:如果不指定getItemLayout prop,則無法滾動到渲染窗口之外的位置。
Parameters:
| 名稱 | 類型 | 必需 | 說明 |
|---|---|---|---|
| params | object | Yes | See below. |
有效的params鍵是:
- 'animated' (boolean) - 列表是否應該在滾動時執行動畫。默認為true。
- 'index' (number) - 要滾動到的索引。需要。
- 'viewOffset' (number) - 用於偏移最終目標位置的固定像素數。需要。
- 'viewPosition' (number) - 值為0時,index指定的項目位於頂部,1位於底部,0.5位於中間。
scrollToItem()
scrollToItem(params);
需要線性掃描數據 - 如果可能,請使用scrollToIndex。
注意:如果不指定getItemLayout prop,則無法滾動到渲染窗口之外的位置。
參數:
| 名稱 | 類型 | 必需 | 說明 |
|---|---|---|---|
| params | object | Yes | See below. |
有效的params鍵是:
- 'animated' (boolean) - 要滾動到的偏移量。在水平為真的情況下,偏移是x值,在任何其他情況下,偏移是y值。需要。
- 'item' (object) - 列表是否應該在滾動時執行動畫。默認為true。
- 'viewPosition' (number)
scrollToOffset()
scrollToOffset(params);
Scroll to a specific content pixel offset in the list.
Parameters:
| 名稱 | 類型 | 必需 | 說明 |
|---|---|---|---|
| params | object | Yes | See below. |
有效的params鍵是:
- 'offset' (number) - 要滾動到的偏移量。在水平為真的情況下,偏移是x值,在任何其他情況下,偏移是y值。需要。
- 'animated' (boolean) - 列表是否應該在滾動時執行動畫。默認為true。
recordInteraction()
recordInteraction();
告訴列表發生了交互,這應該觸發可視性計算,例如,如果waitForInteractions為true且用戶未滾動。這通常通過點擊項目或導航操作來調用。
flashScrollIndicators()
flashScrollIndicators();
暫時顯示滾動指示器。

0 意見