简体   繁体   English

可触摸的不透明度 OnPress 在 React 本机中不起作用

[英]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.

相关问题 将本机onpress变为可触摸的其他模型 - React-native onpress into touchable open another model react-native 如何将图像和onpress添加到touchable - react-native how to add image and onpress into touchable 与 firebase 反应本机在添加消息后崩溃,没有错误,然后在删除可触摸不透明度后不起作用 - react native with firebase crashed with no error after adding messaging and then after removing touchable opacity is not working 在 FlatList 内 2 到 5 秒后 React Native 可触摸不透明度按下 - React Native touchable opacity pressing after 2 to 5 seconds inside a FlatList React Native TouchableHighlight onPress 不起作用 - React Native TouchableHighlight onPress not working React Native:Touchable Opacity 元素在 iOS 上可点击,但在 Android 上不可点击 - React Native: Touchable Opacity element is clickable on iOS but not Android React Native Overflow Touchable 在 Android 中不起作用 - React Native Overflow Touchable is Not Working in Android 为什么 Touchable Opacity 在 android 上不起作用? - Why is Touchable Opacity not working on android? 反应本机 TouchableOpacity onPress 在 Android 上不起作用 - React native TouchableOpacity onPress not working on Android React Native:文本 onPress 在 android 上不起作用 - React Native: Text onPress not working on android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM