反应本机监听组件外部的触摸事件

react native listen to touch event outside component

我正在创建一个可滑动视图组件列表。我想实现的是,当一个可滑动项处于活动状态时,触摸除此可滑动视图之外的任何其他地方都会关闭滑动。有点像 textInput onblur.

我认为添加侦听器可能会有所帮助,但不确定如何实现。

稍后我还有一个堆栈导航器,它将把一个页面推到列表的顶部。我正在寻找的是相同的,一旦在视图之外,然后关闭滑动。

代码是这样的,要清楚。

<SafeAreaView>
  <ScrollView>
  {items.map(item => <CustomItem itemdata={item} />)}
  </ScrollView>
</SafeAreaView>

自定义项目组件如:

import Swipeable from 'react-native-gesture-handler/Swipeable';

....//React component stuff

render() {
  return (
  <Swipeable
    friction={2}
    overshootRight={false}
    renderRightActions={this.renderRight}
  >
    <View> 
    .....
    </View>
  </Swipeable>
  );
}

在您的组件旁边,添加 TouchableOpacity 以占用显示屏的其余部分


render() {
 return (
      <>
       <TouchableOpacity style={{flex: 1, height: "100%", width:"100%"}} onPress={()=>yourOutsdieClickAction()} />
       <Swipeable
          friction={2}
          overshootRight={false}
          renderRightActions={this.renderRight}
        >
        <View> 
        .....
        </View>
       </Swipeable>
      </>
      );
  }