react-native scrollview 不滚动
react-native scrollview doesn't scroll
我似乎不明白为什么我的 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>
);
}
}
有些项目超出了视口的下限。但是在模拟器中触摸拖动滚动没有任何作用。我做错了什么?
如果 scrollview 之外没有视图,则删除 View 元素并将 flex:1 添加到 scrollview。
滚动视图应该是父视图,
<ScrollView style={styles.container}>
<View style={{flex:1}}>
{deficiencies}
</View>
</ScrollView>
container: {
flex: 1,
}
我的 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
});
我似乎不明白为什么我的 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>
);
}
}
有些项目超出了视口的下限。但是在模拟器中触摸拖动滚动没有任何作用。我做错了什么?
如果 scrollview 之外没有视图,则删除 View 元素并将 flex:1 添加到 scrollview。
滚动视图应该是父视图,
<ScrollView style={styles.container}>
<View style={{flex:1}}>
{deficiencies}
</View>
</ScrollView>
container: {
flex: 1,
}
我的 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
});