简体   繁体   English

反应原生滚动视图不在android上滚动

[英]react native scrollview not scrolling on android

I have the following component:我有以下组件:

export default class StoreComponent extends Component {
  render() {
    return (
      <View style={styles.container}>
        <ScrollView contentContainerStyle={styles.scroll}>
          <StoreCarouselComponent />
          <StoreDiscountComponent />
          <StoreDetailsComponent />
        </ScrollView>
      </View>
    );
  }
}

with this style用这种风格

import { StyleSheet, Dimensions, } from 'react-native';

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#ffffff',
  },
  scroll: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'center'
  },
  image: {
    width: Dimensions.get('window').width,
    height: 350,
  },
  box: {
    width: Dimensions.get('window').width - 30,
    position: 'absolute',
    shadowColor: '#000000',
    shadowOpacity: 0.34,
    shadowRadius: 5,
    shadowOffset: {
      width: 0,
      height: 10
    },
    elevation: 10,
    borderTopLeftRadius: 10,
    borderTopRightRadius: 10,
    borderBottomLeftRadius: 10,
    borderBottomRightRadius: 10,
    borderColor: 'lightgrey',
    backgroundColor: '#ffffff',
    padding: 10,
    marginTop: 410,

  },
  boxDiscount: {
    width: Dimensions.get('window').width - 30,
    position: 'absolute',
    shadowColor: '#000000',
    shadowOpacity: 0.34,
    shadowRadius: 5,
    shadowOffset: {
      width: 0,
      height: 10
    },
    elevation: 10,
    borderTopLeftRadius: 10,
    borderTopRightRadius: 10,
    borderBottomLeftRadius: 10,
    borderBottomRightRadius: 10,
    borderColor: 'lightgrey',
    backgroundColor: '#253241',
    padding: 10,
    marginTop: 320,
  },
  title: {
    fontSize: 30
  },
  distance: {
    fontSize: 20,
    color: '#767676'
  },
  distanceElement: {
    fontSize: 20,
    color: '#44D9E6'
  },
  address: {
    fontSize: 20,
    color: '#767676'
  },
  category: {
    fontSize: 20,
    color: '#767676',
  },
  categoryElement: {
    fontSize: 20,
    color: '#44D9E6',
  },
  hr: {
    borderBottomColor: 'lightgrey',
    borderBottomWidth: 1,
  },
  icons: {
    flex: 1,
    flexDirection: 'row',
    justifyContent: 'center',
  }
});

export default styles;

my scrollview works on ios but on android don't and I don't understand why我的滚动视图可以在 ios 上运行,但在 android 上不行,我不明白为什么

here a an image of the app and as you can see I need to scroll on android:这是应用程序的图像,正如您所看到的,我需要在 android 上滚动:

在此处输入图片说明

flexGrow : 1使用flexGrow : 1而不是flex:1

style={styles.container}使用flex: 1并将其从.scroll删除

I had a similar issue.我有一个类似的问题。 The culprit turned out to be contentContainerStyle={{flex: 1}} on my ScrollView.罪魁祸首原来是我的 ScrollView 上的contentContainerStyle={{flex: 1}} Removing that (It turned out to be unnecessary, anyway.) fixed the problem on Android.删除它(无论如何,事实证明这是不必要的。)解决了Android上的问题。

try to import from尝试从

import { ScrollView } from 'react-native-gesture-handler';

instead of from而不是来自

'react native' '反应原生'

the styles on your scroll contentContainerStyle is uneccessary滚动 contentContainerStyle 上的样式是不必要的

try to remove : styles.scroll尝试删除:styles.scroll

and just give padding or margin on component if you to center it如果您将其居中,只需在组件上提供填充或边距

if its row add props horizontal = true on ScrollView.如果它的行在 ScrollView 上添加道具水平 = true。

To achieve vertical scrolling on a content that's spread horizontally: 要在水平传播的内容上实现垂直滚动:

return (
    <View style={styles.container}>
      <ScrollView>
        <View
          style={{
            display: 'flex',
            flexDirection: 'row',
            justifyContent: 'center',
          }}
        >
          <StoreCarouselComponent />
          <StoreDiscountComponent />
          <StoreDetailsComponent />
        </View>
      </ScrollView>
    </View>
  );

I too had the similar structure as mentioned in question.我也有问题中提到的类似结构。 I had 3 direct children View inside ScrollView and it wasn't scrolling.我在ScrollView有 3 个直接子View并且它没有滚动。 I tried all above solutions with flex, flexGrow etc. Nothing worked for me.我用 flex、flexGrow 等尝试了上述所有解决方案。没有任何效果对我有用。

What worked for me is wrapping all 3 direct children to another View , so for ScrollView there's only one direct children.对我有用的是将所有 3 个直接子级包装到另一个View ,因此对于ScrollView ,只有一个直接子级。

// THIS DOES NOT SCROLL
<ScrollView>
  <View>// 1st direct children</View>
  <View>// 2nd direct children</View>
  <View>// 3rd direct children</View>
</ScrollView>


// THIS WILL SCROLL, As now ScrollView has only one direct children
<ScrollView>
  <View>
    <View>// 1st children</View>
    <View>// 2nd children</View>
    <View>// 3rd children</View>
  </View>
</ScrollView>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM