gpt4 book ai didi

react-native - 在 ListView react-native 中选择一行以获取详细信息

转载 作者:行者123 更新时间:2023-12-04 19:41:53 28 4
gpt4 key购买 nike

我是 react-native 的新手。我在 https://facebook.github.io/react-native/docs/sample-application-movies.html 看到了一个例子.
它正在构建一个电影应用程序,可以获取电影并将它们显示在 ListView 中。我们如何选择一行以获取其详细数据并将其显示在其他 View 中?请帮忙。谢谢 :)

这是我到目前为止所做的:

/**
* Sample React Native App
* https://github.com/facebook/react-native
*/

import React, {
Component,
} from 'react';
import {
AppRegistry,
Image,
ListView,
StyleSheet,
Text,
View,
Navigator,
TouchableOpacity,
} from 'react-native';

var REQUEST_URL = 'https://raw.githubusercontent.com/facebook/react-native/master/docs/MoviesExample.json';

var SCREEN_WIDTH = require('Dimensions').get('window').width;
var BaseConfig = Navigator.SceneConfigs.FloatFromRight;

var CustomLeftToRightGesture = Object.assign({}, BaseConfig.gestures.pop, {

snapVelocity: 8,
edgeHitWidth: SCREEN_WIDTH,
});

var CustomSceneConfig = Object.assign({}, BaseConfig, {
springTension: 100,
springFriction: 1,
gestures: {
pop: CustomLeftToRightGesture,
}
});

var PageOne = React.createClass({
getInitialState:function(){
return{
dataSource: new ListView.DataSource({
rowHasChanged: (row1, row2) => row1 !== row2,
}),
loaded: false,
}
},

componentDidMount() {
this.fetchData();
},

fetchData() {
fetch(REQUEST_URL)
.then((response) => response.json())
.then((responseData) => {
this.setState({
dataSource: this.state.dataSource.cloneWithRows(responseData.movies),
loaded: true,
});
})
.done();
},

render() {
if (!this.state.loaded) {
return this.renderLoadingView();
}

return (
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderMovie}
style={styles.listView}
/>
);
},

renderLoadingView() {
return (
<View style={styles.container}>
<Text>
Loading movies...
</Text>
</View>
);
},

renderMovie(movie) {
title1 = movie.title;
year1 = movie.year;

return (
<TouchableOpacity onPress={this._handlePressList}>
<View style={styles.container}>
<Image
source={{uri: movie.posters.thumbnail}}
style={styles.thumbnail}
/>
<View style={styles.rightContainer}>
<Text style={styles.title}>{movie.title}</Text>
<Text style={styles.year}>{movie.year}</Text>
</View>
</View>
</TouchableOpacity>
);
},

_handlePressList(){
this.props.navigator.push({id: 2, title1, year1});
},
});

var PageTwo = React.createClass({
render(){
return(
<View style={styles.rightContainer}>
<Text style={styles.title}>{title1}</Text>
<Text style={styles.year}>{year1}</Text>
</View>
)
}
})
class SampleAppMovies extends Component{
_renderScene(route, navigator) {
if (route.id === 1) {
return <PageOne navigator={navigator} />
} else if (route.id === 2) {
return <PageTwo navigator={navigator} />
}
}

_configureScene(route) {
return CustomSceneConfig;
}

render() {
return (
<Navigator
initialRoute={{id: 1, }}
renderScene={this._renderScene}
configureScene={this._configureScene} />
);
}
}

var styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'row',
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
rightContainer: {
flex: 1,
},
title: {
fontSize: 20,
marginBottom: 8,
textAlign: 'center',
},
year: {
textAlign: 'center',
},
thumbnail: {
width: 53,
height: 81,
},
listView: {
paddingTop: 20,
backgroundColor: '#F5FCFF',
},
});

module.exports = SampleAppMovies;

最佳答案

在您的 renderRow方法(在您的情况下 renderMovie ),只需将电影传递到 onPress TouchableOpacity 的 Prop ,像这样:

renderMovie(movie) {
title1 = movie.title;
year1 = movie.year;

return (
<TouchableOpacity onPress={() => _handlePressList(movie)}> // SEE HERE!!
<View style={styles.container}>
<Image
source={{uri: movie.posters.thumbnail}}
style={styles.thumbnail}
/>
<View style={styles.rightContainer}>
<Text style={styles.title}>{movie.title}</Text>
<Text style={styles.year}>{movie.year}</Text>
</View>
</View>
</TouchableOpacity>
);
},

_handlePressList(movie){
this.props.navigator.push({id: 2, movie.title1, movie.year1});
},

然后你可以用 movie 做任何你想做的事情.

希望它有帮助。

关于react-native - 在 ListView react-native 中选择一行以获取详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37915301/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com