react-native scrollview不会滚动

时间:2017-10-28 03:54:17

标签: android react-native scrollview

我似乎无法弄清楚为什么我的Android应用中的屏幕在Android模拟器中根本无法滚动。这是我的代码:

import React, { Component } from 'react';
import Dimensions from 'Dimensions';

import {
  Text,
  ScrollView,
  View,
  TouchableHighlight,
  StyleSheet
} from 'react-native';


const win = Dimensions.get('window');

class DeficiencyItem extends Component {

    render() {
        const touchable = {
                width:win.width/2-20,
                height:230,
                backgroundColor:'red',
              };
        return (
            <TouchableHighlight style={touchable}>
                <View>
                    <Text>Item Here</Text>
                </View>
            </TouchableHighlight>
        );
  }
}

export default class Items extends Component {
  static navigationOptions = {title:'hey'};

    constructor(props)
    {
        super(props);
    }

    render() {

        let deficiencies = [];
        for(let x = 0; x<12;x++)
        {
            let row = <DeficiencyItem key={x} />;
            deficiencies.push(row);
        }
        return (
            <View style={{flex:1}}>
              <ScrollView style={{
                margin:5,
                flexWrap: 'wrap',
                flexDirection:'row',
              }}>
               {deficiencies}
              </ScrollView>
            </View>
        );
  }
}

有些项目超出了视口的下限。但触摸拖动以在模拟器中滚动什么都不做。我做错了什么?

2 个答案:

答案 0 :(得分:5)

如果滚动视图外没有视图,则删除View元素并将flex:1添加到scrollview。

Scrollview应该是父视图,

<ScrollView style={styles.container}>
  <View style={{flex:1}}>
    {deficiencies}
  </View>
</ScrollView>

container: {
    flex: 1,
  }

答案 1 :(得分:0)

我的ScrollView嵌套在其他两个视图中。通过向滚动视图提供高度来解决该问题。在iOS上没有必要,因此,在添加了Platform和Dimensions以响应本机导入后,我执行以下操作:

 const { height } = Dimensions.get("screen");
 const styles = StyleSheet.create({
 scrollView: {
   // my outer views have flex:1 so no flex needed here
   alignItems: "center",
   justifyContent: "center", // or whatever you want
   width: "100%,
   height: Platform.OS === "android" ? height * 1.3 : height,
   // if it was just "height", there would be no extra space to scroll on android
 },

 // lots of other styles and stuff

 });