React Native Grid View:Flexbox wrap 不起作用

IT技术 ios gridview reactjs react-native
2021-05-14 18:40:37

React Native 0.32 中似乎存在一个错误。以下代码在 0.20 和 0.24 中运行良好,您可以在 RN Play 链接中看到。

https://rnplay.org/apps/W5k6Xg

'use strict';

var React = require('react');
var ReactNative = require('react-native');
var {
  AppRegistry,
  ListView,
  StyleSheet,
  Text,
  View,
  Image
} = ReactNative;

var GridLayoutExample = React.createClass({

  getInitialState: function() {
    var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
    return {
      dataSource: ds.cloneWithRows([
        { name: 'John', image: 'https://d30y9cdsu7xlg0.cloudfront.net/png/619232-84.png' },
        { name: 'Joel', image: 'https://d30y9cdsu7xlg0.cloudfront.net/png/619230-84.png' },
        { name: 'James', image: 'https://d30y9cdsu7xlg0.cloudfront.net/png/619168-84.png' },
        { name: 'Jimmy', image: 'https://d30y9cdsu7xlg0.cloudfront.net/png/619130-84.png' },
        { name: 'Jackson', image: 'https://d30y9cdsu7xlg0.cloudfront.net/png/619098-84.png' },
        { name: 'Jillian', image: 'https://d30y9cdsu7xlg0.cloudfront.net/png/618793-84.png' },
        { name: 'Julie', image: 'https://d30y9cdsu7xlg0.cloudfront.net/png/618803-84.png' },
        { name: 'Devin', image: 'https://d30y9cdsu7xlg0.cloudfront.net/png/618706-84.png' }
      ])
    };
  },

  render: function() {
    return (
      // ListView wraps ScrollView and so takes on its properties.
      // With that in mind you can use the ScrollView's contentContainerStyle prop to style the items.
        <ListView contentContainerStyle={styles.list}
          dataSource={this.state.dataSource}
          renderRow={this._renderRow}
        />
    );
  },

  _renderRow: function(rowData: string, sectionID: number, rowID: number) {
    return (
        <View>
          <View style={styles.row}>
            <Image style={styles.thumb} source={{uri: rowData.image}} />
            <Text style={styles.text}>
              {rowData.name}
            </Text>
          </View>
        </View>
    );
  }
});


var styles = StyleSheet.create({
  list: {
    justifyContent: 'center',
    flexDirection: 'row',
    flexWrap: 'wrap',
    backgroundColor: "blue"
  },
  row: {
    justifyContent: 'center',
    padding: 5,
    width: 100,
    height: 100,
    backgroundColor: '#F6F6F6',
    borderWidth: 1,
    borderColor: '#CCC',
    alignItems: 'center'
  },
  thumb: {
    width: 64,
    height: 64
  },
  text: {
    marginTop: 5,
    fontWeight: 'bold'
  }
});

AppRegistry.registerComponent('SampleApp', () => GridLayoutExample);

预期输出(如您在 RNPlay 上看到的带有本机 0.24.1 的react):

预期结果

我所看到的(react-native 0.32):

错误的结果。 只能看到 3 个单元格,其余的没有环绕

请帮我解决这个问题。

1个回答

你应该添加alignItems: 'flex-start'到你list的风格。

list: {
  justifyContent: 'center',
  flexDirection: 'row',
  flexWrap: 'wrap',
  alignItems: 'flex-start',
  backgroundColor: "blue"
},

React Native 0.28有一个重大变化,改变了以下行为flex-wrap

由于性能调整flexWrap: wrap不再与alignItems: 'stretch'(默认)一起使用如果您使用,flexWrap: wrap您可能还想添加alignItems: 'flex-start'样式。