在 React Native 中按下按钮时如何更改 FlatList 项目背景颜色
How can i change the FlatList item background color when button is pressed in React Native
假设我有一个包含一些项目的 FlatList,我按下其中一个,然后打开另一个屏幕,其中包含该项目的详细信息。好的,所以我需要的是,在按下名为 "Got it!" 的按钮并进入后屏幕(FlatList 屏幕)后,如何将所选行的背景颜色设置为绿色?
So i click in one item of the FlatList, then it shows me another screen with the details of that item
Once im in the Details screen, i press the button "Got it!", and it brings me back to the FlatList screen
This is exactly what i need, set a background color only in the View shown in that item, if i press another item and do the same thing, it will be shown changed the background both of them, and so on...
注意:class 详细信息和 ProfileActivity 在 App.js 作为孩子时在里面。
class ProfileActivity extends Component
{
GetFlatListItem (Description, Tutorial, Imagen, Title) {
Alert.alert(Description);
this.props.navigation.navigate('Fourth', {Tutorial, Imagen, Title});
}
return(
<View style = { styles.MainContainer }>
<Button title="Logout" onPress={ () => goBack(null) } />
<Text style = {styles.TextComponentStyle}> { this.props.navigation.state.params.Email } </Text>
<FlatList
data={ this.state.dataSource }
ItemSeparatorComponent = {this.FlatListItemSeparator}
renderItem={({item}) => <View style={{flex: 1, flexDirection: 'row'}}> <Text style={styles.points}>+ {item.points}</Text>
<Text style={styles.flatview} onPress={this.GetFlatListItem.bind
(this, item.description, item.tutorial, item.image, item.title)} >{item.title}</Text></View>}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
class Details extends Component{
onPress = () => {
this.setState({
const {goBack} =this.props.navigation;
})
}
return(
<View style = { styles.MainContainer }>
<TouchableHighlight
style={styles.buttonStyle}
onPress={this.onPress}
>
<Text> Got it! </Text>
</TouchableHighlight>
<Text style = {styles.TextComponentStyle}> { this.props.navigation.state.params.Title } </Text>
<Image
style={{width: 66, height: 58}}
source={{uri: this.props.navigation.state.params.Imagen}}
/>
<Text style = {styles.TextComponentStyle}> { this.props.navigation.state.params.Tutorial } </Text>
</View>
);
}
export default MainProject = createStackNavigator(
{
First: { screen: App },
Second: { screen: ProfileActivity },
Fourth: { screen: Details }
});
我认为是在详细信息 class 中将一些值传递给 onPress() 方法,但我不知道具体是哪一个以及如何传递。有人可以帮助我吗?
您必须创建 this.state.someArrayName 并从 this.props.navigation.state.params
复制并向每个对象添加 backgroundColor
键并添加您的颜色。将该颜色应用于项目的视图背景颜色。 *{{背景颜色:item.backgroundColor}}
创建一个函数changeColor
来改变backgroundColor
的颜色
在您的 GetFlatListItem
函数中,将该函数传递给详细视图
在详细信息视图中,调用 changeColor
函数。 this.props. changeColor()
当您点击 Got it
按钮时。
假设我有一个包含一些项目的 FlatList,我按下其中一个,然后打开另一个屏幕,其中包含该项目的详细信息。好的,所以我需要的是,在按下名为 "Got it!" 的按钮并进入后屏幕(FlatList 屏幕)后,如何将所选行的背景颜色设置为绿色?
So i click in one item of the FlatList, then it shows me another screen with the details of that item
Once im in the Details screen, i press the button "Got it!", and it brings me back to the FlatList screen
This is exactly what i need, set a background color only in the View shown in that item, if i press another item and do the same thing, it will be shown changed the background both of them, and so on...
注意:class 详细信息和 ProfileActivity 在 App.js 作为孩子时在里面。
class ProfileActivity extends Component
{
GetFlatListItem (Description, Tutorial, Imagen, Title) {
Alert.alert(Description);
this.props.navigation.navigate('Fourth', {Tutorial, Imagen, Title});
}
return(
<View style = { styles.MainContainer }>
<Button title="Logout" onPress={ () => goBack(null) } />
<Text style = {styles.TextComponentStyle}> { this.props.navigation.state.params.Email } </Text>
<FlatList
data={ this.state.dataSource }
ItemSeparatorComponent = {this.FlatListItemSeparator}
renderItem={({item}) => <View style={{flex: 1, flexDirection: 'row'}}> <Text style={styles.points}>+ {item.points}</Text>
<Text style={styles.flatview} onPress={this.GetFlatListItem.bind
(this, item.description, item.tutorial, item.image, item.title)} >{item.title}</Text></View>}
keyExtractor={(item, index) => index.toString()}
/>
</View>
);
class Details extends Component{
onPress = () => {
this.setState({
const {goBack} =this.props.navigation;
})
}
return(
<View style = { styles.MainContainer }>
<TouchableHighlight
style={styles.buttonStyle}
onPress={this.onPress}
>
<Text> Got it! </Text>
</TouchableHighlight>
<Text style = {styles.TextComponentStyle}> { this.props.navigation.state.params.Title } </Text>
<Image
style={{width: 66, height: 58}}
source={{uri: this.props.navigation.state.params.Imagen}}
/>
<Text style = {styles.TextComponentStyle}> { this.props.navigation.state.params.Tutorial } </Text>
</View>
);
}
export default MainProject = createStackNavigator(
{
First: { screen: App },
Second: { screen: ProfileActivity },
Fourth: { screen: Details }
});
我认为是在详细信息 class 中将一些值传递给 onPress() 方法,但我不知道具体是哪一个以及如何传递。有人可以帮助我吗?
您必须创建 this.state.someArrayName 并从
this.props.navigation.state.params
复制并向每个对象添加backgroundColor
键并添加您的颜色。将该颜色应用于项目的视图背景颜色。 *{{背景颜色:item.backgroundColor}}创建一个函数
changeColor
来改变backgroundColor
的颜色在您的
GetFlatListItem
函数中,将该函数传递给详细视图在详细信息视图中,调用
changeColor
函数。this.props. changeColor()
当您点击Got it
按钮时。