How to Wrap Flatlist items in React Native
Asked Answered
K

4

9

I am trying to render the list of items like in this image. enter image description here

Items in each row will vary based on their text size. Flatlist is using for rendering items.

TagView.js

<View style={styles.tagView}>
    <FlatList 
        scrollEventThrottle={1900} 
        data={this.state.interests} 
        numColumns={5}
        renderItem={({ item }) => (
           <View style={styles.tag}>
               <Text>{item.tagName}</Text>
           </View>
        )}
        keyExtractor={(item, index) => index}
        contentContainerStyle={{ paddingBottom: 100 }}
    />
</View>

Style

tagView: {
    flex: 1,
    flexDirection: "row",
    flexWrap: "wrap"
},
tag: {
    borderWidth: 1,
    borderRadius: 10,
    borderColor: "black",
    backgroundColor: "white",
    padding: 3,
    marginTop: 5
}

Result

enter image description here

But here items are not wrapping with device width. Is there any to wrap the contents?

Kezer answered 13/8, 2018 at 10:19 Comment(3)
Also try adding alignItems: 'flex-start' to the parentLeakage
@PritishVaidya Tried. Not workingKezer
I think they don't support it in FlatList for numerous reasons, check here. Alternative would be to use ScrollViewLeakage
W
15

FlatList with columnWrapperStyle

<FlatList 
    columnWrapperStyle={styles.tagView}
    scrollEventThrottle={1900} 
    data={this.state.interests} 
    numColumns={5}
    renderItem={({ item }) => (
       <View style={styles.tag}>
           <Text>{item.tagName}</Text>
       </View>
    )}
    keyExtractor={(item, index) => index}
    contentContainerStyle={{ paddingBottom: 100 }}
/>

Style

tagView: {
  flexWrap: "wrap"
},
Wera answered 4/6, 2020 at 13:44 Comment(1)
This should be accepted as best answerTrencher
V
4

Add Horizontal Prop and try,

      <FlatList 
        scrollEventThrottle={1900} 
        data={this.state.interests} 
        numColumns={5}
        horizontal={false}
        renderItem={({ item }) => (
           <View style={styles.tag}>
               <Text>{item.tagName}</Text>
           </View>
        )}
        keyExtractor={(item, index) => index}
        contentContainerStyle={{ paddingBottom: 100 }}
    />
Victorvictoria answered 13/8, 2018 at 12:11 Comment(4)
horizontal prop is using for rendering items next to each other horizontally instead of stacked vertically.Kezer
Yes so if you give horizontal={false} and numColumns={5}, it will give you a row with 5 values and then wrap.Victorvictoria
@YogarajSaravanan Actually, using both props raises an error: numColumns does not support horizontalSelfdeprecating
This can still cause overlap with the last item in the row and the edge of the screen depending on the size of the items. Anyway to make sure this doesn't happen?Tuba
E
1

Use ScrollView component for this purpose.

<ScrollView contentContainerStyle={{ flexDirection: 'row', flexWrap: 'wrap' }}>
  {tags.map(({ id, name }) => (
     <Text key={id}>{name}</Text>
  ))}
</ScrollView>
Effusion answered 9/6, 2020 at 13:0 Comment(1)
II can confirm that <scrollview> is the good way. I had the same question and change my Flatlist by ScroolView and it works perfectly. Thank you Michael.Brook
A
0

try scroolview and flatlist.

                <ScrollView
                  horizontal
                  showsVerticalScrollIndicator={false}
                  showsHorizontalScrollIndicator={false}
                  contentContainerStyle={{
                    flexDirection: 'row',
                    flexWrap: 'wrap',
                  }}>
                  <FlatList
                    data={this.state.carouselItems}
                    renderItem={this.renderItem}
                    keyExtractor={item => `${item.id_news}`}
                    showsHorizontalScrollIndicator={false}
                    numColumns={this.state.carouselItems.length / 2}
                  />
                </ScrollView>
Anthropomorphosis answered 12/8, 2021 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.