React Native Flatlist TouchableOpacity OnPress 不适用于 Android

Sas*_*thi 6 react-native

有人可以帮我解决这个问题吗?印刷机在 Flatlist 渲染功能中不起作用。这是我的代码。我检查了其他屏幕触摸屏工作正常,但当我在 Flatlist 中尝试时它不起作用



render(){
            return (
                <View style={styles.container} >
                          <FlatList
                                data={this.state.categories}
                                numColumns={2}
                                keyExtractor={item => item.id}
                                renderItem={item => this.renderItem(item)}
                            />
  </View>

 );
}

renderItem = ({item,index}) => {
        return (
          
            <TouchableOpacity onPress={() => this.moveToLocation(item.id)} style={styles.items}>
                <Image source={{uri:item.img}}
                    resizeMode="center"
                    style={styles.itemsimg} />
                <Text style={{textAlign:'center',fontSize:20, }} onPress={() => this.moveToLocation(item.id)}>{item.name}</Text>
            </TouchableOpacity>
            
        );
    }
Run Code Online (Sandbox Code Playgroud)

小智 3

您需要将行元素(在 renderItem 方法内)包装在标签内。TouchableWithoutFeedback 将 onPress 作为它的支柱,您可以在其中提供 onPress 事件。

对于 TouchableWithoutFeedback 请参阅 https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html