admin管理员组文章数量:1384229
I want to update the style of a ListView item when that item is pressed so that the enduser is aware that he/she selected an item.
Listview:
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderFriend}
/>
Row renderer:
renderFriend(friend) {
return (
<TouchableHighlight onPress={ ??? }>
<View style={styles.friendItem}>
<View style={styles.profilePictureContainerNoBorder}>
<Image
source={{uri: '/' + friend.id + '/picture?width=500&height=500'}}
style={styles.profilePicture}
/>
</View>
<Text style={styles.profileName}>{friend.name}</Text>
</View>
</TouchableHighlight>
);
}
How could I change the style of the second View when the user activates the TouchableHighlight?
I would also like to add the selected object to an array of selected objects.
I want to update the style of a ListView item when that item is pressed so that the enduser is aware that he/she selected an item.
Listview:
<ListView
dataSource={this.state.dataSource}
renderRow={this.renderFriend}
/>
Row renderer:
renderFriend(friend) {
return (
<TouchableHighlight onPress={ ??? }>
<View style={styles.friendItem}>
<View style={styles.profilePictureContainerNoBorder}>
<Image
source={{uri: 'https://graph.facebook./' + friend.id + '/picture?width=500&height=500'}}
style={styles.profilePicture}
/>
</View>
<Text style={styles.profileName}>{friend.name}</Text>
</View>
</TouchableHighlight>
);
}
How could I change the style of the second View when the user activates the TouchableHighlight?
I would also like to add the selected object to an array of selected objects.
Share Improve this question asked Dec 6, 2015 at 14:26 Philippe MaesPhilippe Maes 51010 silver badges28 bronze badges2 Answers
Reset to default 6You should use the ponent state and store the selected friends ids in it when pressing the TouchableHighlight
.
Something like:
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./' + friend.id + '/picture?width=500&height=500'}}
style={styles.profilePicture}
/>
</View>
<Text style={styles.profileName}>{friend.name}</Text>
</View>
</TouchableHighlight>
);
}
in functional ponents for return selected element I use option onPress={()=>onTouch(i)}
'i' is a array index
for example for default list:
export const HomePage = () => {
const list = [
{
title: 'FFFFFFFFFF',
icon: 'timer',
idx: 1
},
{
title: 'AAAAAAAAAAAAAAA',
icon: 'place',
idx: 2
},
{
title: 'BBBBBBBBB',
icon: 'info',
idx: 3
},
];
const onTouch = (idTouch) => {
console.log(idTouch);
};
return (
<ScrollView>
<View style={styles.section}>
<Text style={styles.text}>TEXT1</Text>
<Text style={styles.text2}>tEXT2</Text>
{list.map((item, i) => (
<TouchableOpacity on activeOpacity={0.7} key={i+1} style={styles.item} onPress={()=>onTouch(i)}>
<ListItem key={i} bottomDivider >
<Icon name={item.icon} />
<ListItem.Content >
<ListItem.Title>{item.title}</ListItem.Title>
</ListItem.Content>
<ListItem.Chevron />
</ListItem>
</TouchableOpacity>
))}
</View>
</ScrollView>
);
};
本文标签: javascriptReact native change style of ListView item on touchStack Overflow
版权声明:本文标题:javascript - React native: change style of ListView item on touch - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744535805a2611308.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论