React native 在React Native中按下按钮时,如何更改平面列表项的背景色

React native 在React Native中按下按钮时,如何更改平面列表项的背景色,react-native,react-native-android,react-native-ios,react-native-flatlist,react-native-navigation,React Native,React Native Android,React Native Ios,React Native Flatlist,React Native Navigation,假设我有一个包含一些项目的平面列表,我按下其中一个,然后打开另一个包含该项目详细信息的屏幕。好的,我需要的是,在按下名为“Got it!”的按钮并进入后屏幕(FlatList屏幕)后,如何将所选行的背景色设置为绿色 注意:类详细信息和ProfileActivity作为一个孩子在App.js中 class ProfileActivity extends Component { GetFlatListItem (Description, Tutorial, Imagen, Title) {

假设我有一个包含一些项目的平面列表,我按下其中一个,然后打开另一个包含该项目详细信息的屏幕。好的,我需要的是,在按下名为“Got it!”的按钮并进入后屏幕(FlatList屏幕)后,如何将所选行的背景色设置为绿色

注意:类详细信息和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 ProfileActivity扩展组件
{
GetFlatListItem(说明、教程、图像、标题){
警报。警报(说明);
this.props.navigation.navigate('Fourth',{Tutorial,Imagen,Title});
}
返回(
goBack(空)}/>
{this.props.navigation.state.params.Email}
+{item.points}
{item.title}
keyExtractor={(项,索引)=>index.toString()}
/>
);
类详细信息扩展组件{
onPress=()=>{
这是我的国家({
const{goBack}=this.props.navigation;
})
}
返回(
知道了!
{this.props.navigation.state.params.Title}
{this.props.navigation.state.params.Tutorial}
);
}
导出默认MainProject=createStackNavigator(
{
第一:{screen:App},
第二:{screen:ProfileActivity},
第四:{屏幕:详细信息}
});
我想的是在Details类中将一些值传递给方法onPress(),但我不知道具体是哪一个值以及如何传递。有人能帮我吗?

  • 您必须创建this.state.someArrayName并从
    this.props.navigation.state.params
    复制,然后将
    backgroundColor
    键添加到每个对象并添加颜色。将该颜色应用于项目的视图背景色*{{backgroundColor:item.backgroundColor}

  • 创建一个函数
    changeColor
    来更改
    backgroundColor

  • GetFlatListItem
    函数中,将该函数传递给详细视图

  • 在详细视图上,调用
    changeColor
    函数<代码>this.props。changeColor()当您点击
    获取它时


您好,问题是如果未按下“Got it!”按钮,则颜色必须相同。