首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法单击另一个TouchableOpacity内的堆叠TouchableOpacity

无法单击另一个TouchableOpacity内的堆叠TouchableOpacity
EN

Stack Overflow用户
提问于 2017-06-16 23:31:58
回答 3查看 13.4K关注 0票数 21

尽管本文档(https://facebook.github.io/react-native/docs/gesture-responder-system.html)指出,触摸事件被传递给子对象,并且只由父对象使用,但是如果子对象没有对事件做出反应,我就会面对这样的问题:嵌套在另一个TouchableOpacity中的TouchableOpacity不能正确地对接触做出反应。

我的结构如下

代码语言:javascript
复制
<ScrollView>
  <TouchableOpacity onPress={() => console.log('This is printed always')}>
    <View>
      <Text>I can click here</Text>
      <TouchableOpacity onPress={() => console.log('This is printed never')}>
        <Text>I can click here but the outer onPress is called instead of the inner one</text>
      </TouchableOpacity>
    </View>
  </TouchableOpacity>
</ScrollView>

TouchableOpacitys中的按钮也是如此:单击按钮将调用父TouchableOpacity的onPress方法

我在监督什么吗?

EN

回答 3

Stack Overflow用户

发布于 2020-04-25 16:35:21

从以下位置更改可触摸不透明度的导入:

代码语言:javascript
复制
import { TouchableOpacity } from 'react-native-gesture-handler';

下面的代码,现在一切都会好的:

代码语言:javascript
复制
import { TouchableOpacity } from 'react-native';
票数 32
EN

Stack Overflow用户

发布于 2019-11-15 13:57:43

写在这里是为了让它更突出一点。

正如@EliezerSteinbock所提到的,可能是嵌套的TouchableOpacity是从不同于父对象的内容导入的。这是很有可能的,因为我们中的许多人在可视化代码或其他IDE上使用自动导入。

遗憾的是,我第一次错过了她的评论,所以希望这能对其他人有所帮助

票数 3
EN

Stack Overflow用户

发布于 2018-01-25 19:19:15

您可以只使用TouchableWithoutFeedback包装内部的TouchableOpacity

类似于:

代码语言:javascript
复制
<TouchableOpacity onPress={() => console.log('This is printed always')}>
    <View>
        <Text>I can click here</Text>
        <TouchableWithoutFeedback>
            <TouchableOpacity onPress={() => console.log('This is printed never')}>
                <Text>I can click here but the outer onPress is called instead of the inner one</text>
            </TouchableOpacity>
        </TouchableWithoutFeedback>
    </View>
</TouchableOpacity>
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44593024

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档