gpt4 book ai didi

android - 如何处理点击 GridView react native android?

转载 作者:行者123 更新时间:2023-11-29 19:55:51 24 4
gpt4 key购买 nike

我已经在 React Native 中实现了 grid view。它从 json 中获取数据并显示。现在我想点击 grid item 并根据 grid 当前位置的数据显示内容。

Implemented grid view using

最佳答案

您可以将任何 Touchable 组件与 onPress 处理程序一起使用。

例如TouchableHighlight .

您可以将导航器作为 Prop 传递,如下例的 renderScene 部分所示。

index.ios.js

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

import GridView from 'react-native-grid-view';
import Movie from './Movie.js';
import MovieDetail from './MovieDetail.js';

class ReactNativeGridViewTest extends Component {
constructor(props) {
super(props);
this.state = {
movies: ['hello', 'blabla', 'BatMan', 'SuperMan']
};
}
onHandleItemPress(item){
this.refs.navigator.push({
id: 'detail',
data:item
});
}
renderItem(item) {
return (
<TouchableHighlight onPress={this.onHandleItemPress.bind(this, item)}>
<View>
<Movie item={item}/>
</View>
</TouchableHighlight>
);
}
renderScene(route,navigator){
switch(route.id){
case 'master': return (
<View style={styles.container}>
<GridView
items={this.state.movies}
itemsPerRow={3}
renderItem={this.renderItem.bind(this)}
/>
</View>
)
case 'detail': return (<MovieDetail navigator={navigator} data={route.data}/>)
}
}
render() {
return (
<Navigator ref="navigator"
initialRoute={{id: 'master'}}renderScene={this.renderScene.bind(this)}/>
);
}
}

const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

AppRegistry.registerComponent('ReactNativeGridViewTest', () => ReactNativeGridViewTest);

电影.js

import React, {
AppRegistry,
Component,
StyleSheet,
Text,
View,
TouchableHighlight,
} from 'react-native';

export default class Movie extends Component{
render(){
return (
<View style={{width:40, height:40, margin: 20, backgroundColor:'red'}}>
<Text>{this.props.item}</Text>
</View>
)
}
}

MovieDetail.js

import React, {
AppRegistry,
Component,
StyleSheet,
Text,
View,
TouchableHighlight,
} from 'react-native';

export default class MovieDetail extends Component{
render() {
return (
<View style={styles.container}>
<TouchableHighlight onPress={()=> this.props.navigator.pop()}>
<Text>Go Back </Text>
</TouchableHighlight>
<View style={{width:40, height:40, margin: 20, backgroundColor:'red'}}>
<Text>{this.props.data}</Text>
</View>
</View>
);
}
}


const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#F5FCFF',
},
welcome: {
fontSize: 20,
textAlign: 'center',
margin: 10,
},
instructions: {
textAlign: 'center',
color: '#333333',
marginBottom: 5,
},
});

您还可以查看 working example on Github .

关于android - 如何处理点击 GridView react native android?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36741272/

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