react native怎么获取屏幕滚动高度
1个回答
2016-10-25 · 知道合伙人软件行家
关注
展开全部
首先我们来看一下官方的实际例子,不过该代码整体封装比较强,可能对于初学者来说有点难度,具体代码如下:
'use strict'
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
TouchableOpacity,
ScrollView,
} from 'react-native'
var NUM_ITEMS = 20;
var PerfectProject = React.createClass({
Statics: {
titles: '<ScrollView>',
description: 'Component that enable scrolling through child component',
},
makeItems: function(nItems: number, styles) : Array<any> {
var items = [];
for (var i=0;i<nItems;i++){
items[i] = (
<TouchableOpacity key={i} style={styles}>
<Text style={{fontSize:20 }}>{'Item text' + i}</Text>
</TouchableOpacity>
);
}
return items;
},
render: function(){
var items = this.makeItems(NUM_ITEMS,styles.itemWrapper);
items[4] = (
<ScrollView key={'ScrollView'} horizontal={true}>
{this.makeItems(NUM_ITEMS,[styles.itemWrapper,styles.horizontalItemWrapper])}
</ScrollView>
);
var verticalScrollView=(
<ScrollView style={styles.verticalScrollView}>
{items}
</ScrollView>
);
return verticalScrollView;
},
});
var styles = StyleSheet.create({
itemWrapper: {
backgroundColor: '#dddddd',
alignItems: 'center',
borderWidth: 5,
borderColor: 'red',
borderRadius: 5,
padding: 30,
margin: 5,
},
horizontalItemWrapper: {
padding: 50,
},
verticalScrollView:{
backgroundColor: 'aquamarine',
margin: 10,
},
});
'use strict'
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Text,
View,
Image,
TouchableOpacity,
ScrollView,
} from 'react-native'
var NUM_ITEMS = 20;
var PerfectProject = React.createClass({
Statics: {
titles: '<ScrollView>',
description: 'Component that enable scrolling through child component',
},
makeItems: function(nItems: number, styles) : Array<any> {
var items = [];
for (var i=0;i<nItems;i++){
items[i] = (
<TouchableOpacity key={i} style={styles}>
<Text style={{fontSize:20 }}>{'Item text' + i}</Text>
</TouchableOpacity>
);
}
return items;
},
render: function(){
var items = this.makeItems(NUM_ITEMS,styles.itemWrapper);
items[4] = (
<ScrollView key={'ScrollView'} horizontal={true}>
{this.makeItems(NUM_ITEMS,[styles.itemWrapper,styles.horizontalItemWrapper])}
</ScrollView>
);
var verticalScrollView=(
<ScrollView style={styles.verticalScrollView}>
{items}
</ScrollView>
);
return verticalScrollView;
},
});
var styles = StyleSheet.create({
itemWrapper: {
backgroundColor: '#dddddd',
alignItems: 'center',
borderWidth: 5,
borderColor: 'red',
borderRadius: 5,
padding: 30,
margin: 5,
},
horizontalItemWrapper: {
padding: 50,
},
verticalScrollView:{
backgroundColor: 'aquamarine',
margin: 10,
},
});
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询