【问题标题】:React-native: how to wrap FlatList itemsReact-native:如何包装 FlatList 项目
【发布时间】:2017-08-05 21:08:08
【问题描述】:

我有一个查询返回的术语列表。每一个都是一个词。目前,我的 FlatList 将每个单词呈现为同一行上的一个按钮(horizo​​ntal={true}) 我希望按钮像普通文本一样换行。但我绝对不想使用列功能,因为那样看起来不太自然。 这可能是 FlatList 的一个坏用例吗?还有其他我可以使用的组件吗?

    const styles = StyleSheet.create({
      flatlist: {
        flexWrap: 'wrap'
      },
      content: {
        alignItems: 'flex-start'
      }})

    render() {
        return (
          <Content>
            <Header searchBar rounded iosStatusbar="light-content" androidStatusBarColor='#000'>
              <Item>
                <Icon name="ios-search" />
                <Input onChangeText={(text) => this.setState({searchTerm: text})} value={this.state.searchTerm} placeholder="enter word"/>
                <Icon name="ios-people" />

                <Button transparent onPress={this._executeSearch}>
                <Text>Search</Text>
              </Button>
              </Item>
            </Header>

            <FlatList style={styles.flatlist} contentContainerStyle={styles.content}
                horizontal={true} data={this.state.dataSource} renderItem={({item}) =>
                      <Button>
                        <Text>{item.key}</Text>
                      </Button>
                  }>
            </FlatList>
          </Content>
        );
      }

我收到的一条错误消息是:

警告:VirtualizedList 组件不支持flexWrap: wrap``。请考虑将numColumnsFlatList 一起使用。

【问题讨论】:

    标签: react-native react-native-flatlist react-native-flexbox


    【解决方案1】:

    我如何包装组件如下所示

    flatlist: {
       flexDirection: 'column',
    },
    

    在我的 FlatList 组件中添加了这个道具

    numColumns={3}
    

    【讨论】:

    • 要在SectionLlist 中使用numColumns,请参阅stackoverflow.com/questions/47833581/…
    • 此解决方案仅在您不需要即时计算 numColumns 时才有效,因为渲染后无法更改 numColumns。
    • 他们明确表示他们不想使用列功能。这是可以理解的,因为它们的项目宽度不均匀。这不应该是公认的答案。
    • 嗨@Ger。感谢您的评论。你能分享链接吗,我很久以前回答过这个问题,所以如果有什么改变,我想知道。
    • 我指的是上面提出的问题。
    【解决方案2】:

    可以去掉横向的道具来实现包裹效果

    ................
    ..................
    
    <FlatList
        contentContainerStyle={{flexDirection : "row", flexWrap : "wrap"}} 
        data={this.state.dataSource} renderItem={({item}) =>
            <Button>
                <Text>{item.key}</Text>
            </Button>
        }
    />
    .................
    ..............
    

    【讨论】:

    • 接受的答案需要 numColumns,这个答案更好。谢谢
    • 尽管结果在 Android 上看起来令人满意,但这会产生警告 'flexWrap: 'wrap'' is not supported with the 'VirtualList' components. Consider using numColumns with 'FlatList' instead
    【解决方案3】:

    不幸的是,FlatLists 确实不支持 flexWrap。我们建议使用 ScrollView 而不是 FlatList 来创建此效果。这就是问题所在:https://github.com/facebook/react-native/issues/13939

    【讨论】:

      【解决方案4】:

      当我们使用horizontal flatList 时,不能使用flexWrap: wrap,VirtualizedList 组件不支持。您可以使用 numColumns 和 FlatList 在平面列表中创建一些列。然而,

      如果你需要堆叠你可以使用的平面列表项

      contentContainerStyle={{ justifyContent: 'center', flexDirection: 'row', flexWrap: 'wrap' }}
      

      完整代码如下所示

      <FlatList
            data={data}
            keyExtractor={(item) => item.type.id}
            contentContainerStyle={{ justifyContent: 'center', flexDirection: 'row', flexWrap: 'wrap' }}
            renderItem={({ item }) => 
              <Button>
                  <Text>{item.key}</Text>
              </Button>
          }
      />
      

      【讨论】:

        【解决方案5】:
        <FlatList 
                    contentContainerStyle={{ flexDirection: 'row',flexWrap: 'wrap' ,maxWidth:1900,maxHeight:130}}
                    data={data} 
                    horizontal
                    renderItem={({item,index }) => (
                    <ScrollView
                    // contentContainerStyle={{ flexDirection: 'row',flexWrap: 'wrap' ,maxWidth:1900,maxHeight:130}}
                    >
                        <TouchableOpacity
                            key={index}
                                style={apply('row py-1 px-2 bg-white shadow mt-1 mx-1 rounded-lg mb-1')}>
                                <Image
                                    source={{
                                        uri: `${lokasi + item.worker_type_image}`,
                                    }}
                                        style={{
                                            width: (width / 4) - 72,
                                            height: (width / 4) - 72,
                                            opacity: 0.85,
                                            borderRadius:5
                                        }}
                                    />
                                <Text style={apply('p-0 text/4 text-gray-400 mx-2')}>
                                    {item.worker_type_name}
                                </Text>
                        </TouchableOpacity>
                    </ScrollView>
                )}
                keyExtractor={(item, index) => index}
            />
        

        【讨论】:

        • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
        猜你喜欢
        • 2019-01-20
        • 2019-03-17
        • 1970-01-01
        • 1970-01-01
        • 2018-04-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多