ios - react native GridView : Flexbox wrap not working

标签 ios gridview reactjs react-native

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

预期输出(如您在带有 React Native 0.24.1 的 RNPlay 上看到的那样):

Expected result

我看到的(React native 0.32):

Wrong result. Can only see 3 cells and rest of them aren't wrapping

请帮我解决这个问题。

最佳答案

您应该将 alignItems: 'flex-start' 添加到您的 list 样式中。

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

React Native 0.28 中发生了重大变化改变了 flex-wrap 的行为:

Due to performance tweaks flexWrap: wrap no longer works together with alignItems: 'stretch' (the default). If you use flexWrap: wrap you probably will want to add the alignItems: 'flex-start' style as well.

关于ios - react native GridView : Flexbox wrap not working,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39313268/

相关文章:

javascript - 如何禁用某些控件直到更新成功?

android - 如何为日历填充 gridview?

reactjs - Material UI Autocomplete - 在项目选择后禁用列表框

reactjs - 如何在 React Native 中创建功能齐全的帮助文件?

ios - Google App Engine 和 Socket.IO 上的移动聊天应用程序

ios - 不能将字典安全放入变量

iOS - 从 CBPeripheral 对象获取外设的 MAC 地址

ios - 如何在 swift 中制作这样的文本字段?

c# - .Net Gridview 在鼠标悬停后恢复交替颜色

javascript - React JS 如何使用 session 存储在页面之间传递值