如何在React Native中对平面列表的项目执行点击功能

时间:2019-05-23 08:14:59

标签: react-native react-native-flatlist

我正在获取数据列表,当我单击平面列表中的任何项目时,我想显示每个数据的详细信息。

 <FlatList style={{paddingBottom:5}}
          data = {this.state.fetchFreelancer}
          keyExtractor={(y, z) => z.toString()}
          renderItem={({ item }) =>

          <FreelancerCategory

          imageUrifreelancer = {{uri: `${item.profile_img}`}}
          imageUrifeatured = {{uri: `${item.badge.badget_url}`}}
          featuredColor ={`${entities.decode(item.badge.badget_color)}`}
          flagimageUri= {{uri: `${item.location.flag}`}}
          freelancername={`${entities.decode(item.name)}`}
          title={`${entities.decode(item._tag_line)}`}
          rate={`${entities.decode(item._perhour_rate)}`}
          country={`${entities.decode(item.location._country)}`}
          /> 

但是我无法在平面列表中执行onPress,对此有谁可以帮助我。谢谢

它不起作用。

<FlatList
                style={{ paddingBottom: 5 }}
                data={this.state.fetchFreelancer}
                keyExtractor={(y, z) => z.toString()}
                renderItem={({ item }) => (
                  <TouchableOpacity
                 onPress={() => navigate('DetailFreelancerScreen', {profile_id: item.profile_id})}
                //  onPress={() => alert('Item pressed')}
                  >
                    <FreelancerCategory
                      imageUrifreelancer={{ uri: `${item.profile_img}` }}
                      imageUrifeatured={{ uri: `${item.badge.badget_url}` }}
                      featuredColor={`${entities.decode(
                        item.badge.badget_color
                      )}`}
                      flagimageUri={{ uri: `${item.location.flag}` }}
                      freelancername={`${entities.decode(item.name)}`}
                      title={`${entities.decode(item._tag_line)}`}
                      rate={`${entities.decode(item._perhour_rate)}`}
                      country={`${entities.decode(item.location._country)}`}
                    />
                  </TouchableOpacity>
                )}
              />

2 个答案:

答案 0 :(得分:1)

如果要执行点击事件,则应使用TouchableOpacity或TouchableWithoutFeedback覆盖项目。

<FlatList 
    style={{paddingBottom:5}}
    data = {this.state.fetchFreelancer}
    keyExtractor={(y, z) => z.toString()}
    renderItem={({ item }) => (
        <TouchableOpacity
           onPress={() => this.props.navigation.navigate('DetailFreelancerScreen', 
               {profile_id: item.profile_id})}
        >
           <FreelancerCategory
             imageUrifreelancer = {{uri: `${item.profile_img}`}}
             imageUrifeatured = {{uri: `${item.badge.badget_url}`}}
             featuredColor ={`${entities.decode(item.badge.badget_color)}`}
             flagimageUri= {{uri: `${item.location.flag}`}}
             freelancername={`${entities.decode(item.name)}`}
             title={`${entities.decode(item._tag_line)}`}
             rate={`${entities.decode(item._perhour_rate)}`}
             country={`${entities.decode(item.location._country)}`}
          /> 
       </TouchableOpacity>
      );
  />

有关更多信息,请参考RN official document

答案 1 :(得分:0)

请阅读此https://facebook.github.io/react-native/docs/flatlist。本文将帮助您了解如何正确使用FlatList组件。希望对您有帮助