listview的onpress
时间: 2023-11-30 07:04:29 浏览: 76
onPress是一个React Native ListView的事件属性,它在用户点击ListView的行时被触发。当用户点击一行时,该行会被高亮显示并触发onPress事件。开发者可以通过onPress事件中的回调函数来实现自定义操作,例如导航到另一个页面、更新数据等。下面是一个简单的例子,展示如何在ListView中使用onPress事件:
```
import React, { Component } from 'react';
import { Text, View, ListView, StyleSheet } from 'react-native';
export default class MyListView extends Component {
constructor(props) {
super(props);
this.state = {
dataSource: new ListView.DataSource({
rowHasChanged: (r1, r2) => r1 !== r2,
}),
};
}
componentDidMount() {
const data = [
{ id: 1, name: 'John' },
{ id: 2, name: 'Jane' },
{ id: 3, name: 'Bob' },
];
this.setState({
dataSource: this.state.dataSource.cloneWithRows(data),
});
}
renderRow(rowData) {
return (
<View style={styles.row}>
<Text>{rowData.name}</Text>
</View>
);
}
onPress(rowData) {
console.log(`Pressed row with id ${rowData.id}`);
}
render() {
return (
<ListView
dataSource={this.state.dataSource}
renderRow={(rowData) => this.renderRow(rowData)}
onPress={(rowData) => this.onPress(rowData)}
/>
);
}
}
const styles = StyleSheet.create({
row: {
padding: 10,
borderBottomWidth: 1,
borderBottomColor: '#eee',
},
});
```
阅读全文