【问题标题】:FlatList numColumns doesn't appear to be working correctly?FlatList numColumns 似乎无法正常工作?
【发布时间】:2017-04-19 23:58:51
【问题描述】:

我正在尝试使用 FlatList 以网格格式向某人显示一堆用户头像,但它最终看起来很奇怪,我似乎无法弄清楚如何解决它。

Here's what it looks like

这是我的 FlatList 代码的样子:

<FlatList
style={{flex: 1}}
data={this.props.usersList}
horizontal={false}
numColumns={3}
columnWrapperStyle={{ marginTop: 10 }}
renderItem={({ item }) => this.renderItem(item)}
keyExtractor={this._keyExtractor}/>

这是 renderItem 组件的外观:

class UserButton extends React.Component {
render() {
    const { item, onPress } = this.props;
    return (
        <TouchableOpacity style={styles.button} onPress={onPress}>
            <Image
                source={(item.avatar) ? { uri: item.avatar } : require('../assets/images/userplaceholder.png')}
                resizeMode='cover'
                style={styles.imageStyle}
            />
        </TouchableOpacity>
    )
}

const styles = {
    button: {
        height: 100,
        width: 100,
        borderColor: '#aaa',
        backgroundColor: '#aaa',
        borderWidth: 2, 
        borderRadius: 50,
        justifyContent: 'center',
        alignItems: 'center',
        marginHorizontal: 5,
    },
    imageStyle: {
        height: 96,
        width: 96,
        alignSelf: 'center',
        borderRadius: 48,
        marginTop: (Platform.OS == 'android') ? 0 : 0.4
    }
}

export default UserButton;

有人有什么想法吗?

【问题讨论】:

    标签: react-native react-native-flatlist


    【解决方案1】:

    您可以从维度中获取宽度并为平面列表中的项目设置该宽度。

    const {width} = Dimensions.get('window');
    const itemWidth = (width) / 4;
    

    【讨论】:

      【解决方案2】:

      我也遇到过类似的情况,但现在我使用 flatList 有了合适的网格,你可以看看下面附上的我的代码。

      <FlatList
          contentContainerStyle={{margin:4}}
          horizontal={false}
          numColumns={4}
          data={this.state.categoryDataSource}
          renderItem={(categoryItem) =>
              <CategoryListItem category={categoryItem.item} mode="small"/>
              }
          keyExtractor={category => category.id}
      />
      

      【讨论】:

        猜你喜欢
        • 2017-09-04
        • 2012-05-06
        • 2013-02-17
        • 2017-10-09
        • 2017-08-03
        • 2014-05-02
        • 2011-09-11
        • 2016-02-11
        • 1970-01-01
        相关资源
        最近更新 更多