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

标签 javascript android ios react-native mobile-development

我有一个 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/

相关文章:

ios - 为什么表格 View 不显示 iphone 中的任何数据

javascript - 窗口对象 (window.something = something) 在 JavaScript 中做什么?

android - Android webkit源码中的JNI函数

android - 签署 apk 后 android maps apiv2 的问题

ios - 无法创建 iOS 代码签名证书

ios - 动态更改数据源导致 deleteRowsAtIndexPaths :indexes to crash

javascript - map 加载后如何向 Google map 添加标记?

javascript - 为 getScript() 添加超时

javascript - Ajax 数据未在下拉列表中正确绑定(bind)

android - 如何修复我的布局位置错误?