How to render items in two columns in react native?

回眸只為那壹抹淺笑 提交于 2020-01-06 06:46:33

问题


my skills in react native is basic, so i want to render items in two columns, i'm using this library https://github.com/GeekyAnts/react-native-easy-grid.

componentDidMount() {

   return fetch(ConfigApp.URL+'json/data_posts.php')
     .then((response) => response.json())
     .then((responseJson) => {
       this.setState({
         isLoading: false,
         dataSource: responseJson
       }, function() {
       });
     })
     .catch((error) => {
       console.error(error);
     });
 }

Return

 return (
 <Grid>
  <Col><FlatList
      data={ this.state.dataSource }
      refreshing="false"
      renderItem={({item}) => 
            <TouchableOpacity activeOpacity={1}>
            <ImageBackground source={{uri: ConfigApp.IMAGESFOLDER+item.post_image}} style={styles.background_card}>
                <LinearGradient colors={['rgba(0,0,0,0.6)', 'rgba(0,0,0,0.9)']} style={styles.gradient_card}>
                        <Text style={styles.category_card}>{item.category}</Text>
                        <Text style={styles.title_card}>{item.post_title}</Text>
                        <Text style={styles.subcategory_card}>{item.date}</Text>
                </LinearGradient>
            </ImageBackground>
            </TouchableOpacity>
 }
    keyExtractor={(item, index) => index}

    />
 </Col>
 </Grid>
    );

回答1:


Since FlatList already supports numColumns, therefore you can do

Example

const data = [1,2,3,4]


  <FlatList 
         data={data}
         numColumns={2}
         renderItem={(item) => <View style={{flex: 1, height: 200, margin: 5, backgroundColor: 'red'}}/>} // Adding some margin
   />


来源:https://stackoverflow.com/questions/49845628/how-to-render-items-in-two-columns-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!