对本机组件外部的触摸事件进行反应

时间:2020-04-25 17:07:36

标签: javascript reactjs react-native listener react-native-gesture-handler

我正在创建可滑动查看的组件列表。我想实现一种功能,当一个可滑动活动处于活动状态时,触摸此可滑动视图之外的其他任何位置将关闭该滑动。有点像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>
  );
}

1 个答案:

答案 0 :(得分:1)

在您的组件旁边,添加TouchableOpacity并占用其余显示屏幕


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