gpt4 book ai didi

listview - 在 React Native 中更改选择时的列表项背景

转载 作者:行者123 更新时间:2023-12-03 17:16:36 25 4
gpt4 key购买 nike

我是 React-Native 的新手。我想使用 ListView 选择一项。如何更改 ListView 项目背景以显示选择。我正在尝试这样。请帮助。让我知道我在这里做错了什么。它适用于第一次选择,但不再适用。

getRowSelectionStyle(isSelect){
if(isSelect){
return(
{flex: 1, backgroundColor: '#dedede'}
)
}
else{
return(
{flex: 1,backgroundColor: '#ffffff'}
)
}
}

_onPressRow(rowID, rowData) {
// Resetting all Row value of the list
var dataClone = this.state.listData;
for (var i = 0; i < dataClone.length; i++) {
var cloneData = dataClone[i];
if(i == rowID){
cloneData.isSelect = !cloneData.isSelect;
selectedFormIndex = cloneData.isSelect?i:(-1);
}
else{
cloneData.isSelect = false;
}
dataClone[i] = cloneData;
}

this.setState({ listData: dataClone });
this.setState({
dataSource: this.state.dataSource.cloneWithRows(dataClone)
});
}

_renderRow(rowData: string, sectionID: number, rowID: number) {
var formTitle = rowData.row+'('+rowData.formNo+')';
return (
<TouchableHighlight onPress={this._onPressRow.bind(this, rowID, rowData)} >
<View style={this.getRowSelectionStyle(rowData.isSelect)}>
<View style={styles.listItemContainer}>
<View style={styles.listItemTextContainer}>
<Text style={styles.listItemText}>
{formTitle}
</Text>
<Text style={this.getRowFrequencyStyle(rowData.frequency)}>
{rowData.frequency}
</Text>
</View>
<View style={this.getStatusColorStyle(rowData.status)}>
<TouchableHighlight>
<Text style={{color:(rowData.isSelect === false)? '#ffffff':'#222d4a', fontSize: 11}}>
{rowData.status}
</Text>
</TouchableHighlight>
</View>
</View>

<View>
<Text style={styles.listItemDate}>
{'Schedule Start Date: '+rowData.startDate}
</Text>
<Text style={styles.listItemDate}>
{'Schedule End Date: '+rowData.endDate}
</Text>
</View>
</View>
</TouchableHighlight>
);
}


_renderList() {
if(this.state.listData.length == 0){
return (
<View style={{height: 150, justifyContent:'center', alignItems:'center'}}>
<Text style={{fontSize: 16, color:"gray", width:250}}>
No form available for this site. Please select a site.
</Text>
</View>
);
}
else{
return (
<ListView
dataSource={this.state.dataSource}
renderSeparator={this._renderSeparator}
renderRow={this._renderRow.bind(this)}
enableEmptySections={true}
/>
);
}
}


render(){
return(
<View style={styles.listContainer}>
{this._renderList()}
</View>
);
}

最佳答案

您可以使用 underlayColor如 react 原生文档中所述

underlayColor?: color

The color of the underlay that will show through when the touch is active.



所以在你的渲染方法中你可以写成
<TouchableHighlight onPress={this._onPressRow.bind(this, rowID, rowData)} underlayColor={"green"} >        
...
</TouchableHighlight>

希望这可以帮助..

关于listview - 在 React Native 中更改选择时的列表项背景,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44601677/

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