[英]React native: How to make horizontal scroll but data is divided to 4 rows
我需要水平滾動我的項目,但我的數據分為 4 行,每行包含 4 個項目。 我可以水平滾動,以便接下來的 16 個項目出現在屏幕上。
使用 numColumns={4} 時,如果
水平={假}
但與
水平={真}
我無法指定 numColumns attr。
我應該使用 SectionList 而不是 FlatList 嗎?
以及如何實施?
let items = [
{ id: 1, title: '1', price: '20' },
{ id: 2, title: '2', price: '16' },
{ id: 3, title: '3', price: '92' },
{ id: 4, title: '4', price: '93' },
{ id: 5, title: '5', price: '20' },
{ id: 6, title: '6', price: '16' },
{ id: 7, title: '7', price: '92' },
{ id: 8, title: '8', price: '93' },
{ id: 9, title: 'Grilled Steak', price: '20' },
{ id: 10, title: 'Pappas', price: '16' },
{ id: 11, title: 'Ciccione', price: '92' },
{ id: 12, title: 'Gyros Melt', price: '93' },
{ id: 13, title: 'Grilled Steak', price: '20' },
{ id: 14, title: 'Pappas', price: '16' },
{ id: 15, title: 'Ciccione', price: '92' },
{ id: 16, title: 'Gyros Melt', price: '93' },
];
<FlatList
keyExtractor={item => item.id}
data={items}
horizontal
numColumns={4}
renderItem={({ item }) => <Text>{item.title}</Text>}
/>
我對相同的場景有同樣的問題。 如果您有長數組,制作組數據是一種非常糟糕的做法。 所以我玩了 flatlist 的風格,我成功了。 如果您可以利用它,以下是我的代碼:
<ScrollView showsHorizontalScrollIndicator={false} horizontal={true} style={styles.flatListContainer}>
<FlatList
showsHorizontalScrollIndicator={false}
data={item.items}
numColumns={4}
renderItem={_showData}
columnWrapperStyle={styles.flatListColumnWraper}
keyExtractor={(item) => item.key}
style={styles.flatListDataContainer}
scrollEnabled={false}
contentContainerStyle={
styles.flatListContentContainerStyle
}
/>
</ScrollView>
flatListContainer: {
marginTop: height(2),
height:height(60),
paddingVertical:height(2)
},
flatListDataContainer: {
alignSelf: 'center',
},
flatListContentContainerStyle: {
alignSelf: 'center',
flexDirection: 'row',
justifyContent: 'space-evenly',
},
flatListColumnWraper:{
flexDirection: 'column',
},
不幸的是,FlatList 在水平時不支持多列。
https://facebook.github.io/react-native/docs/flatlist#numcolumns
多列只能使用 horizontal={false} 渲染,並且會像 flexWrap 布局一樣呈鋸齒形。 項目都應具有相同的高度 - 不支持磚石布局。
但是,您可以對數據進行分組,以便為水平 FlatList 中的每個單元格呈現 4 個項目。
let items = [
[ { id: 1, title: '1', price: '20' },
{ id: 2, title: '2', price: '16' },
{ id: 3, title: '3', price: '92' },
{ id: 4, title: '4', price: '93' } ],
[ { id: 5, title: '5', price: '20' },
{ id: 6, title: '6', price: '16' },
{ id: 7, title: '7', price: '92' },
{ id: 8, title: '8', price: '93' } ],
[ { id: 9, title: 'Grilled Steak', price: '20' },
{ id: 10, title: 'Pappas', price: '16' },
{ id: 11, title: 'Ciccione', price: '92' },
{ id: 12, title: 'Gyros Melt', price: '93' } ],
[ { id: 13, title: 'Grilled Steak', price: '20' },
{ id: 14, title: 'Pappas', price: '16' },
{ id: 15, title: 'Ciccione', price: '92' },
{ id: 16, title: 'Gyros Melt', price: '93' } ]
];
然后更新你的 renderItem 以便它處理增加的輸入,就像這樣。
renderItem = ({item}) => {
return item.map(i => <Text>{i.title}</Text>)
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.