jjzjj

javascript - FlatList contentContainerStyle -> justifyContent : 'center' causes issues with scrolling

coder 2023-09-24 原文

我有一个 FlatList 组件,我在其中呈现 x 个 TouchableHighlight。我需要 FlatList 中的所有组件垂直居中对齐。

问题是,如果我将 justifyContent: center 放在 contentContainerStyle 中,什么也不会发生,但是如果我将 flex: 1 添加到 contentContainerStyle 我得到我想要的结果。如果我不必进行滚动,这很酷,但是当我在 FlatList 中有许多组件强制滚动以查看所有组件时,滚动从这些列表的中心开始并且不要让我滚动。

这是我的代码:

<FlatList
        style = {{flex: 0.7, backgroundColor: 'red'}}
        data = {this.props.actions}
        contentContainerStyle = {{justifyContent:'center',}}
        keyExtractor={(item, index) => index}
        renderItem = {({item, index})=>{
          return (
            <TouchableHighlight
              style = {{alignItems: 'center', margin: 8, paddingTop: 8, paddingBottom: 8, //flex: 1,
              borderColor: ConstantesString.colorGrisFlojito, backgroundColor: '#d7deeb',
              borderRadius: 10,
            }}
              underlayColor = {ConstantesString.colorAzulTouched}
              onPress = {()=>{
                item.action();
              }}>
              <Text
                style = {{color: '#005288' , textAlign: 'center', fontSize: 20}}
              >
                {item.name}
              </Text>
            </TouchableHighlight>
          )
        }}
      />

我不知道这是一个错误还是我做得不好。

最佳答案

这不是错误,你也没有做坏事,这是预期的行为,因为你正在向 容器flex > ScrollView 的 列表使其占据屏幕的整体高度,因此只能滚动到屏幕的内容

需要用flexGrow代替flex来解决

The flex grow factor of a flex item is relative to the size of the other children in the flex-container

<FlatList
    contentContainerStyle={{flexGrow: 1, justifyContent: 'center'}}
    //... other props
/>

关于javascript - FlatList contentContainerStyle -> justifyContent : 'center' causes issues with scrolling,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50268409/

有关javascript - FlatList contentContainerStyle -> justifyContent : 'center' causes issues with scrolling的更多相关文章

随机推荐