0

Basically, my flatlist is in 2 columns. But if there is an odd number of item the last row will be stretch.

This is my Flatlist coding

export default class Cluster1 extends Component{
render(){
    const getClusterByName = (name) => 
Cluster.find(cluster => cluster.hasOwnProperty(name))[name]
    return(
        <ScrollView>
        <FlatList
        numColumns= {2}
        data={getClusterByName('cluster1')}
        renderItem={({item,index})=>{
            return(
                <FlatListItem  item={item} index={index}>

                </FlatListItem>);
        }}
        >
        </FlatList>
        </ScrollView>
    );
}

Then this is my FlatlistItem

class FlatListItem extends Component{
render(){
    const {itemWidth} = this.props
    return(
        <View style={styles.list}>
        <View>


        <Text style={styles.itemText}>{this.props.item.name}</Text>

        </View>
        </View>
    );
}

style code

   const styles = StyleSheet.create({  
    list:{
    flex:1,
    borderRadius: 4,
    borderWidth: 0.5,
    width: 50,
    padding:20,
    backgroundColor: 'ghostwhite',
    alignItems: 'center',
    justifyContent: 'center',
    margin: 2,


    }

This will be the output as you can see the 'f' is stretch. I want it make the same width enter image description here

2
  • Add your styles.list code
    – Roy Wang
    Commented Apr 27, 2018 at 8:21
  • oh yeah i forgot about it my bad
    – user9673740
    Commented Apr 27, 2018 at 8:29

2 Answers 2

1

Remove flex: 1 from your styles.list as that would cause it to occupy the entire width.

If you want them to occupy half the width then you can change it to flex: 0.5 or the width style to Dimensions.get('window').width / 2.

0
1

set width to your renderItem as Dimensions.get('window').width / 2;

Use this :

    borderRadius: 4,
    borderWidth: 0.5,
    width: (Dimensions.get('window').width - 4 * 10) / 2,
    padding:20,
    backgroundColor: 'ghostwhite',
    alignItems: 'center',
    justifyContent: 'center',
    margin: 2,