我是 React Native 的初学者。正如您从图片中看到的,我有一个滚动视图和两个按钮。我已经成功实现了滚动视图,它工作正常,但我也希望他们在按下按钮时自动滚动。我尝试了很多搜索,但没有得到任何有效的东西。所以任何帮助表示赞赏。请在下面找到我的代码。
import React, { Component } from 'react';
import { AppRegistry, StyleSheet, Text, View, Dimensions, ScrollView, Button } from 'react-native';
var screenWidth = Dimensions.get('window').width;
export default class App extends React.Component {
render() {
return (
<View style={styles.MainContainer}>
<View style={styles.ButtonViewContainer}>
<View style={styles.ButtonContainer}>
<Button title="Screen 1" />
</View>
<View style={styles.ButtonContainer}>
<Button title="Screen 2" />
</View>
</View>
<ScrollView
horizontal={true}
pagingEnabled={true}
showsHorizontalScrollIndicator={false}
>
<View style={styles.ScrollContainer}>
<Text style={styles.ScrollTextContainer}>
Screen 1
</Text>
</View>
<View style={styles.ScrollContainer}>
<Text style={styles.ScrollTextContainer}>
Screen 2
</Text>
</View>
</ScrollView>
</View>
);
}
}
const styles = StyleSheet.create({
MainContainer: {
backgroundColor: '#abe3a8',
flex: 1,
justifyContent: 'center',
alignItems: 'center'
},
ScrollContainer: {
backgroundColor: '#cdf1ec',
flexGrow: 1,
marginTop: 0,
width: screenWidth,
justifyContent: 'center',
alignItems: 'center'
},
ScrollTextContainer: {
fontSize: 20,
padding: 15,
color: '#000',
textAlign: 'center'
},
ButtonViewContainer: {
flexDirection: 'row',
paddingTop: 35,
},
ButtonContainer: {
padding: 30,
},
});