gpt4 book ai didi

javascript - react native : change style of ListView item on touch

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:36:01 24 4
gpt4 key购买 nike

我想在按下该项目时更新 ListView 项目的样式,以便最终用户知道他/她选择了一个项目。

ListView :

<ListView
dataSource={this.state.dataSource}
renderRow={this.renderFriend}
/>

行渲染器:

renderFriend(friend) {
return (
<TouchableHighlight onPress={ ??? }>
<View style={styles.friendItem}>
<View style={styles.profilePictureContainerNoBorder}>
<Image
source={{uri: 'https://graph.facebook.com/' + friend.id + '/picture?width=500&height=500'}}
style={styles.profilePicture}
/>
</View>
<Text style={styles.profileName}>{friend.name}</Text>
</View>
</TouchableHighlight>
);
}

当用户激活 TouchableHighlight 时,如何更改第二个 View 的样式?

我还想将所选对象添加到所选对象数组中。

最佳答案

当按下 TouchableHighlight 时,您应该使用组件状态并将选定的 friend ID 存储在其中。

类似于:

constructor(props) {
super(props);
this.state = {
selectedFriendIds: [],
}
}

selectFriend(friend) {
this.setState({
selectedFriendIds: this.state.selectedFriendIds.concat([friend.id]),
});
}

renderFriend(friend) {
const isFriendSelected = this.state.selectedFriendIds.indexOf(friend.id) !== -1;
const viewStyle = isFriendSelected ?
styles.profilePictureContainerSelected : styles.profilePictureContainerNoBorder;

return (
<TouchableHighlight onPress={ () => this.selectFriend(friend) }>
<View style={styles.friendItem}>
<View style={viewStyle}>
<Image
source={{uri: 'https://graph.facebook.com/' + friend.id + '/picture?width=500&height=500'}}
style={styles.profilePicture}
/>
</View>
<Text style={styles.profileName}>{friend.name}</Text>
</View>
</TouchableHighlight>
);
}

关于javascript - react native : change style of ListView item on touch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34118467/

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