带有 flex 1 的 ScrollView 使其不可滚动

ScrollView with flex 1 makes it un-scrollable

我正在尝试 运行 在 ScrollView 上弯曲,只要 ScrollView 有 flex: 1,内部的 滚动就不起作用. 这是世博会 fiddle(你可以 运行 这段代码来玩) https://snack.expo.io/SySerKNp-

请注意,如果您从 ScrollView 中删除 flex: 1,它确实可以滚动,但您会失去弹性(让红色容器向下推向上框的能力( ScrollView) ) 所以我必须在那里有一个 flex。

p.s - 我只在 android 上工作,我还没有在 iPhone 上测试过(我不介意那里的结果)

知道我错过了什么吗?为什么 ScrollView 在有 flex: 1 时不能正常工作? 谢谢!

我认为您的问题是您告诉 ScrollView 使用 flex=1 占用所有可用 space 但问题是 ScrollView 的工作方式不同。它会自动渲染其所有子项,因此它与 flex 的工作方式不同。这是与具有更好性能的普通 ListView 或 FlatList 的区别。

我相信这个小吃可以解决这个问题:https://snack.expo.io/SkxN9GOT-

基本上,我正在获取设备的高度,并根据(screenHeight - 红色框的当前高度)将 ScrollView 设置为固定高度。

已经提供了如何做。

但是这里有一个解释为什么你不能用你的方法来做。 contentContainerStyle给出的样式是

applied to the scroll view content container which wraps all of the child views.

因此,当您将 flex: 1 应用于 contentContainer 时,它会将高度也为 flex: 1ScrollView 的全高作为其父级 View

你也可以模拟-

the ability to let the red container down to push up the upper box

通过向 ScrollView 添加父项并在父项中应用样式

<View style={styles.root}>
  <View style={{ flex: 1, borderColor: 'green', borderWidth: 5 }}>
    <ScrollView>
      <View style={styles.box1} />
      <View style={styles.box2} />
      <View style={styles.box1} />
    </ScrollView>
  </View>
  <View style={{ height: this.state.height, backgroundColor: 'red' }}>
    <TouchableOpacity onPress={() => this.setState({ height: this.state.height + 10 })}>
      <Text>Click</Text>
    </TouchableOpacity>
  </View>
</View>

尝试在 scrollView 内容容器样式中使用 flexGrow: 1 而不是 flex: 1,如下所示。

<ScrollView contentContainerStyle={{ flexGrow: 1, borderColor: 'green', borderWidth: 5 }}>
  <View style={styles.box1} />
  <View style={styles.box2} />
  <View style={styles.box1} />
</ScrollView>

https://snack.expo.io/HkkEVoh6Z

试试这个,它将 100% 解决您的问题

import React, { Component } from 'react';
import { AppRegistry, View,ScrollView } from 'react-native';

export default class AlignItemsBasics extends Component {
  render() {
    return (
      // Try setting `alignItems` to 'flex-start'
      // Try setting `justifyContent` to `flex-end`.
      // Try setting `flexDirection` to `row`.
      <View style={{ flex: 1,flexDirection: 'column' }}>
        <View style={{   height: 50, backgroundColor: 'powderblue'}} />
        <View style={{  flex: 1,  backgroundColor: 'skyblue'}} >
            <ScrollView>

<View style={{  flexDirection: 'column' , minHeight: 'fit-content'}} >
 <View style={{    height:150, backgroundColor: 'red'}} />
    <View style={{ minHeight: 'fit-content', backgroundColor: '#fe3222' }} />      

 <View style={{    height:150, backgroundColor: '#fff222'}} />
    <View style={{    height:150, backgroundColor: '#555222'}} />           
              </View>

  </ScrollView>
 </View>
      </View>
    );
  }
};

// skip this line if using Create React Native App
AppRegistry.registerComponent('AwesomeProject', () => AlignItemsBasics);

最好的做法是将您的 ScrollView 包裹在一个视图中 并使用 flex 控制该视图,您的滚动视图将随之而来。

这是一个小例子

<View style={{flex: 1, flexDirection: 'column',}}>

          <View style={{flex:5, backgroundColor : 'green' }}>
            <ScrollView style={{margin:50, backgroundColor : 'pink' }}>

              <Text> Hello Scroll View </Text>
              <Text> Hello Scroll View </Text>
              <Text> Hello Scroll View </Text>
              <Text> Hello Scroll View </Text>
              <Text> Hello Scroll View </Text>
              <Text> Hello Scroll View </Text>

            </ScrollView>
          </View>

          <View style={{flex:1, backgroundColor : 'blue' }}>
              <Text> Hello Static View </Text>
          </View>

</View>