Skip to content

Commit 7e597d0

Browse files
authored
[Android] Handle gestures on transparent views directly under the Detector (#3962)
## Description When using the new API, the direct descendant of the Detector component always passes through `shouldHandlerlessViewBecomeTouchTarget`. If that view happens to be transparent, it will not be taken into account during hit testing unless it's not a ViewGroup. This PR adds an explicit check to ensure that views that are direct children of a Detector component are always considered for hit testing, even if they are transparent. ## Test plan ```jsx import React from 'react'; import { StyleSheet, View } from 'react-native'; import { GestureDetector, GestureHandlerRootView, useTapGesture, } from 'react-native-gesture-handler'; export default function EmptyExample() { const tap = useTapGesture({ onActivate: () => { console.log('tap'); }, }); return ( <GestureHandlerRootView style={styles.container}> <GestureDetector gesture={tap}> <View style={{ flex: 1, backgroundColor: 'transparent' }} collapsable={false} /> </GestureDetector> </GestureHandlerRootView> ); } const styles = StyleSheet.create({ container: { flex: 1, }, }); ```
1 parent e99cf63 commit 7e597d0

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

packages/react-native-gesture-handler/android/src/main/java/com/swmansion/gesturehandler/core/GestureHandlerOrchestrator.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import android.view.ViewGroup
88
import android.widget.EditText
99
import com.facebook.react.uimanager.ReactCompoundView
1010
import com.facebook.react.uimanager.RootView
11+
import com.swmansion.gesturehandler.react.RNGestureHandlerDetectorView
1112
import com.swmansion.gesturehandler.react.RNGestureHandlerRootHelper
1213
import com.swmansion.gesturehandler.react.RNGestureHandlerRootView
1314
import com.swmansion.gesturehandler.react.isHoverAction
@@ -745,9 +746,12 @@ class GestureHandlerOrchestrator(
745746

746747
// TODO: this is not an ideal solution as we only consider ViewGroups that has no background set
747748
// TODO: ideally we should determine the pixel color under the given coordinates and return
748-
// false if the color is transparent
749-
val isLeafOrTransparent = view !is ViewGroup || view.getBackground() != null
750-
return isLeafOrTransparent && isTransformedTouchPointInView(coords[0], coords[1], view)
749+
val isLeaf = view !is ViewGroup
750+
val isNotTransparent = view.getBackground() != null
751+
val isDirectDetectorChild = view.parent is RNGestureHandlerDetectorView
752+
val isPointInView = isTransformedTouchPointInView(coords[0], coords[1], view)
753+
754+
return (isLeaf || isNotTransparent || isDirectDetectorChild) && isPointInView
751755
}
752756

753757
fun transformPointToChildViewCoords(x: Float, y: Float, parent: ViewGroup, child: View, outLocalPoint: PointF) {

0 commit comments

Comments
 (0)