ListView grid in React Native

后端 未结 9 1966
长发绾君心
长发绾君心 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:42

    Follow Colin Ramsay's answer. And if you want half width for each item, try this way.

    ...
    import { Dimensions } from 'react-native'; 
    const { width, height } = Dimensions.get('window');
    const gutter = 0; // You can add gutter if you want
    ...
    
    
    const styles = StyleSheet.create({
      item: {
        width: (width - gutter * 3)/2,
        marginBottom: gutter,
        flexDirection: 'column',
        alignSelf: 'flex-start',
        backgroundColor: '#ff0000',
      },
      list: {
        flexDirection: 'row',
        justifyContent: 'space-between',
        flexWrap: 'wrap',
        paddingHorizontal: gutter,
      },
    });
    
    0 讨论(0)
  • 2020-11-28 02:44

    use FlatList instead of Listview in react-native. it has more value added feature and better performance. checkout the example here

    0 讨论(0)
  • 2020-11-28 02:45

    You can use FlatList and set numColumns to obtain the result which is the same with grid

    0 讨论(0)
  • 2020-11-28 02:47

    I had the same problem and I wrote a component that solves that problem, you can find it here: https://github.com/pavlelekic/react-native-gridview

    Also, one other thing this component does is it makes sure that the items width is a whole number, so that the borders of items don't have antialiasing, they are clear and crisp.

    0 讨论(0)
  • 2020-11-28 02:48

    Check the following example using FlatList component of React Native to support infinite scrolling with an excellent performance:

    //Resize the grid
    onLayout = (event) => { 
      const {width} = event.nativeEvent.layout;
      const itemWidth = 150
      const numColumns = Math.floor(width/itemWidth)
      this.setState({ numColumns: numColumns })
    }
    
    render() {
      return (
        <Content 
         contentContainerStyle={{flex:1, alignItems: 'center'}}
         onLayout={this.onLayout}>
           <FlatList
            data={this.state.products}
            keyExtractor={(item, index) => index}
            key={this.state.numColumns}
            numColumns={this.state.numColumns}
            renderItem={({item}) => 
              <ListThumb //Custom component, it's only an example.
               navigation={navigation}
               brand={item.brand}
               price={item.price}
               imageSource={item.image_link}/>
            }
         />
       </Content>
      )
    }    
    

    The example looks like

    Best regards, Nicholls

    0 讨论(0)
  • 2020-11-28 02:49

    I'm adding this as an answer because overflow comments are hidden under Colin Ramsay's response: as of React Native 0.28, you also need alignItems: 'flex-start', in the listview styling or the flexWrap won't work. Thanks to Kerumen on codedump.io for this. So,

    var styles = StyleSheet.create({
    list: {
        flexDirection: 'row',
        flexWrap: 'wrap',
        alignItems: 'flex-start',
    },
    

    ...with the rest as in Colin's response.

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