gpt4 book ai didi

listview - React Native ListView : rendered Text is not selectable

转载 作者:行者123 更新时间:2023-12-03 06:38:03 26 4
gpt4 key购买 nike

我是 React Native 的新手,正在测试 ListView。我将每一行渲染为带有 selectable={true} 的文本,但我无法选择任何行中的任何文本。

我无法找出原因,谷歌搜索也没有给我任何东西。

我应该如何做才能在 ListView 的每一行中选择文本?

我的测试代码:

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

class Item extends Component {
onPress = () => {
console.log(`Item: component pressed: ${this.props.itemObj.name} `);
};

render() {
{/*<TouchableHighlight onPress={this.onPress} underlayColor='greenyellow'>*/}
return (
<TouchableHighlight>
<Text style={styles.item} selectable={true}>
{this.props.itemObj.name}
</Text>
</TouchableHighlight>
);
}
}

export default class ListViewTest extends Component {
constructor(props) {
super(props);
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 != r2 });
this.state = {
dataSource: ds.cloneWithRows([]),
};
}

componentWillMount() {
console.log("componentWillMount");
let items = [];
for (let i=0; i<30; i++) {
items.push({name: "item" + i});
}
this.setState({dataSource: this.state.dataSource.cloneWithRows(items)});
}

renderRow = (itemObj) => {
// console.log("renderRow(): ");
// console.log(row);
return <Item itemObj={itemObj} />;
{/*return <Text style={styles.item} selectable={true}>test message</Text>*/}
};

render() {
console.log("render");
return (
<ScrollView>
<ListView
style={styles.list}
dataSource={this.state.dataSource}
initialListSize={1}
renderRow={this.renderRow}
enableEmptySections={true}
/>
</ScrollView>
);

// return (
// <ScrollView>
// <Item itemObj={{name: "item1"}} />
// </ScrollView>
// );
}
}

const styles = StyleSheet.create({
list: {
flex: 1,
},
item: {
padding: 5,
borderBottomColor : 'blue',
borderStyle: 'solid',
borderBottomWidth : 1,
},
});

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

最佳答案

您可以更改构造函数。我认为您不会将数组加载到listview。您应该删除包含componentWillMount。您可以将项目推送到构造函数内的数组。你能试试这段代码吗?

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

class Item extends Component {
onPress = () => {
console.log(`Item: component pressed: ${this.props.itemObj.name} `);
};

render() {
return (
<View>
<Text style={styles.item} selectable={true}>
{this.props.itemObj.name}
</Text>
</View>
);
}
}

export default class ListViewTest extends Component {
constructor(props) {
super(props);
let items = [];
for (let i=0; i<30; i++) {
items.push({name: "item" + i});
}
const ds = new ListView.DataSource({rowHasChanged: (r1, r2) => r1 != r2 });
this.state = {
dataSource: ds.cloneWithRows([items]),
};
}

renderRow = (itemObj) => {
return <Item itemObj={itemObj} />;
}
};

render() {
console.log("render");
return (
<ScrollView>
<ListView
style={styles.list}
dataSource={this.state.dataSource}
initialListSize={1}
renderRow={this.renderRow}
enableEmptySections={true}
/>
</ScrollView>
);

// return (
// <ScrollView>
// <Item itemObj={{name: "item1"}} />
// </ScrollView>
// );
}
}

const styles = StyleSheet.create({
list: {
flex: 1,
},
item: {
padding: 5,
borderBottomColor : 'blue',
borderStyle: 'solid',
borderBottomWidth : 1,
},
});

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

关于listview - React Native ListView : rendered Text is not selectable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42009275/

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