javascript – React native:触摸时更改ListView项的样式
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了javascript – React native:触摸时更改ListView项的样式,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含1472字,纯文字阅读大概需要3分钟。
内容图文
![javascript – React native:触摸时更改ListView项的样式](/upload/InfoBanner/zyjiaocheng/819/ce86bd3d404d493fa4e173e7cbee3cb3.jpg)
我想在按下该项时更新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时,如何更改第二个视图的样式?
我还想将所选对象添加到所选对象的数组中.
解决方法:
按下TouchableHighlight时,您应该使用组件状态并将选定的朋友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:触摸时更改ListView项的样式全部内容,希望文章能够帮你解决javascript – React native:触摸时更改ListView项的样式所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。