【问题标题】:How to render items in two columns in react native?如何在本机反应中呈现两列中的项目?
【发布时间】:2018-04-15 18:52:43
【问题描述】:

我在 React Native 方面的技能是基本的,所以我想在两列中呈现项目,我正在使用这个库 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 (
 <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>
    );

【问题讨论】:

    标签: reactjs react-native react-native-android react-native-ios


    【解决方案1】:

    由于FlatList已经支持numColumns,所以你可以这样做

    示例

    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
       />
    

    【讨论】:

    • 但是如果列表应该在 ScrollView 中,那么你不能使用 FlatList...
    猜你喜欢
    • 1970-01-01
    • 2019-09-09
    • 1970-01-01
    • 2019-05-13
    • 2021-11-23
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    相关资源
    最近更新 更多