[英]Touchable opacity OnPress not working in React native
TouchableOpacity onPress is not working inside Flatlist but when I replaced onPress with onPressIn/onPressOut it is working fine, but in that case the reaction is too fast and having issue while scrolling. TouchableOpacity onPress 在 Flatlist 中不起作用,但是当我用 onPressIn/onPressOut 替换 onPress 时它工作正常,但在这种情况下,反应太快并且在滚动时出现问题。 I don''t know what it is happening and haven't found any related issue.
我不知道发生了什么,也没有发现任何相关问题。 Below is my code:
下面是我的代码:
renderItem = ({ item, index }: { item: any, index: number }) => {
const { type } = this.props;
const valueType = {
phone: item,
stage: item.title,
location: item.name
}
return (
<TouchableOpacity
onPressIn={() => this.onSelect(item, index)}
style={styles.modalListContainer}
>
<Icon name={icon[type]} height={20} width={20}/>
<Spacer width={10} />
<View style={styles.modelTextContainer}>
<Text style={styles.modelText}>{valueType[type]}</Text>
</View>
</TouchableOpacity>
)
}
<FlatList
data={item}
renderItem={this.renderItem}
keyExtractor={this.keyExtractor}
ItemSeparatorComponent={() => <><Spacer height={10} /><View style={styles.modelTextDevider} /><Spacer height={10} /></>}
showsVerticalScrollIndicator={false}
contentContainerStyle={styles.container}
/>
It is rendered inside a Modal using react-native-modals library.它使用 react-native-modals 库在 Modal 中呈现。 Any help would be appreciated.
任何帮助,将不胜感激。 Thank you.
谢谢你。
react-native-modals
, have a parent touchable component (PanResponder) which wraps your children's components. react-native-modals
,有一个父可触摸组件(PanResponder),它包装了您孩子的组件。 On some android devices, when you have a touchable component like a button, the touch event does not propagate down to child component instead capture by react-native-modals
parent component.在某些 android 设备上,当您有一个像按钮这样的可触摸组件时,触摸事件不会向下传播到子组件,而是由
react-native-modals
父组件捕获。
The ideal solution should be absolute positioning your button but will break your UI and the modal will be useless.理想的解决方案应该是绝对定位您的按钮,但会破坏您的 UI,并且模式将毫无用处。
There's an existing issue with this library repository.此库存储库存在一个现有问题。 https://github.com/jacklam718/react-native-modals/pull/210 but the solution provided is not 100% accurate for Android devices.
https://github.com/jacklam718/react-native-modals/pull/210但针对 Android 设备提供的解决方案并非 100% 准确。
If you're using React Navigation, you already installed react-native-gesture-handler
.如果您使用的是 React Navigation,则您已经安装了
react-native-gesture-handler
。
import TouchableOpacity from react-native-gesture-handler
in place of `react-native.从
react-native-gesture-handler
导入 TouchableOpacity 代替 `react-native. It should solve the issue for most devices.它应该可以解决大多数设备的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.