Skip to content

Commit cfdf4b1

Browse files
author
Piotr Trocki
committed
fix nested pager view gestures
1 parent d6e1062 commit cfdf4b1

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

android/src/main/java/com/reactnativepagerview/ComposePagerView.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.reactnativepagerview
22

33
import android.content.Context
4+
import android.view.MotionEvent
45
import android.view.View
56
import android.view.ViewGroup
67
import android.widget.FrameLayout
@@ -38,6 +39,7 @@ class ComposePagerView(context: Context) : FrameLayout(context) {
3839
private val layoutDirectionState = mutableStateOf(LayoutDirection.Ltr)
3940
private val pageMarginState = mutableStateOf(0)
4041
private val offscreenPageLimitState = mutableStateOf(0)
42+
private val sameOrientationChildGestureState = mutableStateOf(false)
4143
private var initialPage = 0
4244
private var didEmitInitialPage = false
4345
private var currentPage = 0
@@ -72,13 +74,23 @@ class ComposePagerView(context: Context) : FrameLayout(context) {
7274
}
7375

7476
override fun onDetachedFromWindow() {
77+
updateSameOrientationAncestorsGestureState(false)
7578
if (composeView.parent === this) {
7679
super.removeView(composeView)
7780
didSetContent = false
7881
}
7982
super.onDetachedFromWindow()
8083
}
8184

85+
override fun dispatchTouchEvent(event: MotionEvent): Boolean {
86+
when (event.actionMasked) {
87+
MotionEvent.ACTION_DOWN -> updateSameOrientationAncestorsGestureState(true)
88+
MotionEvent.ACTION_UP,
89+
MotionEvent.ACTION_CANCEL -> updateSameOrientationAncestorsGestureState(false)
90+
}
91+
return super.dispatchTouchEvent(event)
92+
}
93+
8294
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
8395
super.onMeasure(widthMeasureSpec, heightMeasureSpec)
8496
measureComposeView()
@@ -186,6 +198,21 @@ class ComposePagerView(context: Context) : FrameLayout(context) {
186198
pageMarginState.value = value
187199
}
188200

201+
private fun setSameOrientationChildGestureActive(value: Boolean) {
202+
sameOrientationChildGestureState.value = value
203+
}
204+
205+
private fun updateSameOrientationAncestorsGestureState(value: Boolean) {
206+
val orientation = orientationState.value
207+
var ancestor = parent
208+
while (ancestor != null) {
209+
if (ancestor is ComposePagerView && ancestor.orientationState.value == orientation) {
210+
ancestor.setSameOrientationChildGestureActive(value)
211+
}
212+
ancestor = (ancestor as? View)?.parent
213+
}
214+
}
215+
189216
fun setCurrentItem(selectedPage: Int, animated: Boolean) {
190217
if (selectedPage < 0 || pages.isNotEmpty() && selectedPage >= pages.size) {
191218
return
@@ -310,7 +337,7 @@ class ComposePagerView(context: Context) : FrameLayout(context) {
310337
val reverseLayout = layoutDirectionState.value == LayoutDirection.Rtl
311338
val pageSpacing = pageMarginState.value.dp
312339
val beyondViewportPageCount = offscreenPageLimitState.value
313-
val userScrollEnabled = scrollEnabledState.value
340+
val userScrollEnabled = scrollEnabledState.value && !sameOrientationChildGestureState.value
314341

315342
if (orientationState.value == Orientation.Vertical) {
316343
VerticalPager(

0 commit comments

Comments
 (0)