React 本机 Flatlist TouchableOpacity OnPress 无法正常工作 Android
React native Flatlist TouchableOpacity OnPress Not Working On Android
有人能帮我解决这个问题吗?新闻在 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>
);
}
您需要将行元素(在您的 renderItem 方法内)包装在标记内。 TouchableWithoutFeedback 以 onPress 为道具,您可以在其中提供 onPress 事件。
TouchableWithoutFeedback 参考
https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html
如果 onPress 在 flatlist 渲染方法中不起作用,请尝试使用组件的 onTouchStart 方法(如果它们在 flatList 渲染方法中有)。
有人能帮我解决这个问题吗?新闻在 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>
);
}
您需要将行元素(在您的 renderItem 方法内)包装在标记内。 TouchableWithoutFeedback 以 onPress 为道具,您可以在其中提供 onPress 事件。
TouchableWithoutFeedback 参考 https://facebook.github.io/react-native/docs/touchablewithoutfeedback.html
如果 onPress 在 flatlist 渲染方法中不起作用,请尝试使用组件的 onTouchStart 方法(如果它们在 flatList 渲染方法中有)。