@@ -8,6 +8,7 @@ import android.view.ViewConfiguration
88import android.widget.FrameLayout
99import androidx.viewpager2.widget.ViewPager2
1010import androidx.viewpager2.widget.ViewPager2.ORIENTATION_HORIZONTAL
11+ import com.facebook.react.uimanager.events.NativeGestureUtil
1112import kotlin.math.absoluteValue
1213import kotlin.math.sign
1314
@@ -27,6 +28,7 @@ class NestedScrollableHost : FrameLayout {
2728 private var touchSlop = 0
2829 private var initialX = 0f
2930 private var initialY = 0f
31+ private var nativeGestureStarted: Boolean = false
3032 private val parentViewPager: ViewPager2 ?
3133 get() {
3234 var v: View ? = parent as ? View
@@ -57,17 +59,14 @@ class NestedScrollableHost : FrameLayout {
5759 }
5860
5961 private fun handleInterceptTouchEvent (e : MotionEvent ) {
60- val orientation = parentViewPager?.orientation ? : return
61-
62- // Early return if child can't scroll in same direction as parent
63- if (! canChildScroll(orientation, - 1f ) && ! canChildScroll(orientation, 1f )) {
64- return
65- }
62+ val orientation = parentViewPager?.orientation
6663
6764 if (e.action == MotionEvent .ACTION_DOWN ) {
6865 initialX = e.x
6966 initialY = e.y
70- parent.requestDisallowInterceptTouchEvent(true )
67+ if (orientation != null ) {
68+ parent.requestDisallowInterceptTouchEvent(true )
69+ }
7170 } else if (e.action == MotionEvent .ACTION_MOVE ) {
7271 val dx = e.x - initialX
7372 val dy = e.y - initialY
@@ -78,6 +77,10 @@ class NestedScrollableHost : FrameLayout {
7877 val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1f else .5f
7978
8079 if (scaledDx > touchSlop || scaledDy > touchSlop) {
80+ NativeGestureUtil .notifyNativeGestureStarted(this , e)
81+ nativeGestureStarted = true
82+
83+ if (orientation == null ) return
8184 if (isVpHorizontal == (scaledDy > scaledDx)) {
8285 // Gesture is perpendicular, allow all parents to intercept
8386 parent.requestDisallowInterceptTouchEvent(false )
@@ -94,4 +97,14 @@ class NestedScrollableHost : FrameLayout {
9497 }
9598 }
9699 }
100+
101+ override fun onTouchEvent (e : MotionEvent ): Boolean {
102+ if (e.actionMasked == MotionEvent .ACTION_UP ) {
103+ if (nativeGestureStarted) {
104+ NativeGestureUtil .notifyNativeGestureEnded(this , e)
105+ nativeGestureStarted = false
106+ }
107+ }
108+ return super .onTouchEvent(e)
109+ }
97110}
0 commit comments