React Native Flat列表列

时间:2018-04-27 08:16:46

标签: react-native

基本上,我的平面列表分为两列。但如果有一个奇数项目,最后一行将会拉伸。

这是我的Flatlist编码

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>
    );
}

然后这是我的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>
    );
}

样式代码

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


    }

这将是输出,因为你可以看到'f'是拉伸。我希望它的宽度相同 https://jsfiddle.net/ewolden/wyp8Lfx0/1/

2 个答案:

答案 0 :(得分:0)

flex: 1移除styles.list,因为这会占用整个宽度。

如果您希望它们占据宽度的一半,则可以将其更改为flex: 0.5或将width样式更改为Dimensions.get('window').width / 2

答案 1 :(得分:0)

renderItem设置为Dimensions.get('window').width / 2;

使用此:

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