Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you evenly distribute columns in a FlatList?

I have a grid-like FlatList that is 3x3. I want the 3 columns to be distributed evenly based on the container (screen) width. In a normal view this would be accomplished by justifyContent: 'space-between', but this doesn't do anything when used through contentContainerStyle.

<FlatList
  contentContainerStyle={{ justifyContent: 'space-between' }}
  horizontal={false}
  scrollEnabled={false}
  numColumns={3}
  data={this.props.icons}
  extraData={this.props.selectedIcon}
  renderItem={this.renderItem}
  keyExtractor={this.keyExtractor}
/>

As you can see the icons are all pushed to the left.

enter image description here

like image 443
Kevin_TA Avatar asked Dec 05 '17 01:12

Kevin_TA


People also ask

How do I put space between items in FlatList?

Just add horizontal padding to your container. and no matter how big your items get you will always have spacing left and right. If the item gets too big you can start to scroll.

How do you give space in FlatList React Native?

You can give the item itself a width value of 45% . Also, flatlist has a property called columnWrapperStyle that you can give the value justifyContent: 'space-between . For me, space-around looks better than space-between.

What is keyExtractor in FlatList?

It extracts the unique key name and its value and tells the FlatList component to track the items based on that value. For the above array of data, modify the FlatList component and use the keyExtractor prop to extract the key: <FlatList data={DATA_WITH_ID} renderItem={renderList} keyExtractor={item => item.


1 Answers

I solved this by passing a "columnWrapperStyle" to the FlatList:

<FlatList
    data={array}
    renderItem={this.renderItem}
    numColumns={10}
    columnWrapperStyle={style.row}
/>

const style = StyleSheet.create({
  row: {
    flex: 1,
    justifyContent: "space-around"
  }
});
like image 99
Juanso87 Avatar answered Sep 19 '22 12:09

Juanso87