ListView grid in React Native

后端 未结 9 1967
长发绾君心
长发绾君心 2020-11-28 02:18

I\'m building a simple app in React Native that fetches listings from a remote JSON source and displays them on screen.

So far, using the excellent example here, I\

相关标签:
9条回答
  • 2020-11-28 02:57

    ScrollView with contentContainerStyle prop worked for me

            <ScrollView contentContainerStyle={{ flexDirection: "row", flexWrap: "wrap", alignContent: "center",}}>
                {user.posts.map((post, index) => (
                    <Image
                        source={require(post.uri)}
                        key={index}
                        style={{ width: 100, height: 100 }}
                    />
                ))}
            </ScrollView>
    
    0 讨论(0)
  • 2020-11-28 02:58

    You need to use a combination of flexbox, and the knowledge that 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.

    var TestCmp = React.createClass({
        getInitialState: function() {
          var ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2});
          var data = Array.apply(null, {length: 20}).map(Number.call, Number);
          return {
            dataSource: ds.cloneWithRows(data),
          };
        },
    
        render: function() {
          return (
            <ListView contentContainerStyle={styles.list}
              dataSource={this.state.dataSource}
              renderRow={(rowData) => <Text style={styles.item}>{rowData}</Text>}
            />
          );
        }
    });
    

    Just a ListView with some dummy data. Note the use of contentContainerStyle. Here's the style object:

    var styles = StyleSheet.create({
        list: {
            flexDirection: 'row',
            flexWrap: 'wrap'
        },
        item: {
            backgroundColor: 'red',
            margin: 3,
            width: 100
        }
    });
    

    We tell the container we want items in a wrapping row, and the we set the width of each child object.

    Screenshot

    0 讨论(0)
  • 2020-11-28 03:03

    ListView is now deprecated and you should use FlatList instead. FlatList has a prop called numColumns which is exactly what we want to create a scrollable grid.

    For example:

    const data = [
      {id: 'a', value: 'A'},
      {id: 'b', value: 'B'},
      {id: 'c', value: 'C'},
      {id: 'd', value: 'D'},
      {id: 'e', value: 'E'},
      {id: 'f', value: 'F'},
    ];
    const numColumns = 3;
    const size = Dimensions.get('window').width/numColumns;
    const styles = StyleSheet.create({
      itemContainer: {
        width: size,
        height: size,
      },
      item: {
        flex: 1,
        margin: 3,
        backgroundColor: 'lightblue',
      }
    });
    
    function Grid(props) {
      return (
        <FlatList
          data={data}
          renderItem={({item}) => (
            <View style={styles.itemContainer}>
              <Text style={styles.item}>{item.value}</Text>
            </View>
          )}
          keyExtractor={item => item.id}
          numColumns={numColumns} />
      );
    }
    

    This blog post does a good job explaining the new features of FlatList.

    Note: For some reason you have to use keyExtractor on the FlatList instead of the typical key prop on each item. Otherwise you'll get a warning. Basic FlatList code throws Warning - React Native

    0 讨论(0)
提交回复
热议问题