简体   繁体   English

React Native:如何进行水平滚动但数据分为4行

[英]React native: How to make horizontal scroll but data is divided to 4 rows

I need to scroll my items horizontally but my data is divided to 4 rows every row contain 4 items.我需要水平滚动我的项目,但我的数据分为 4 行,每行包含 4 个项目。 and I can scroll horizontally so the next 16 item come to the screen.我可以水平滚动,以便接下来的 16 个项目出现在屏幕上。

when using numColumns={4} it works if使用 numColumns={4} 时,如果

horizontal={false}水平={假}

but with但与

horizontal={true}水平={真}

I can't specify numColumns attr.我无法指定 numColumns attr。

Should I use SectionList instead of FlatList ?我应该使用 SectionList 而不是 FlatList 吗?

and how it could be implemented ?以及如何实施?

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>}
/>

I have the same issue with same scenario.我对相同的场景有同样的问题。 making group data is quite a bad practice in case you have long array.如果您有长数组,制作组数据是一种非常糟糕的做法。 so I play with styles of flatlist and I was successful.所以我玩了 flatlist 的风格,我成功了。 following is my code if you can take advantage out of it:如果您可以利用它,以下是我的代码:

  <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',
 },

Unfortunately FlatList doesn't support a number of columns when it is horizontal.不幸的是,FlatList 在水平时不支持多列。

https://facebook.github.io/react-native/docs/flatlist#numcolumns https://facebook.github.io/react-native/docs/flatlist#numcolumns

Multiple columns can only be rendered with horizontal={false} and will zig-zag like a flexWrap layout.多列只能使用 horizo​​ntal={false} 渲染,并且会像 flexWrap 布局一样呈锯齿形。 Items should all be the same height - masonry layouts are not supported.项目都应具有相同的高度 - 不支持砖石布局。

However, you could group your data so that for every cell in a horizontal FlatList 4 items are rendered.但是,您可以对数据进行分组,以便为​​水平 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' } ]
];

Then update your renderItem so that it handles the increased input, something like this.然后更新你的 renderItem 以便它处理增加的输入,就像这样。

 renderItem = ({item}) => {
   return item.map(i => <Text>{i.title}</Text>)
 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM