|
1 | 1 | package com.reactnativepagerview |
2 | 2 |
|
3 | 3 | import android.content.Context |
| 4 | +import android.view.MotionEvent |
4 | 5 | import android.view.View |
5 | 6 | import android.view.ViewGroup |
6 | 7 | import android.widget.FrameLayout |
@@ -38,6 +39,7 @@ class ComposePagerView(context: Context) : FrameLayout(context) { |
38 | 39 | private val layoutDirectionState = mutableStateOf(LayoutDirection.Ltr) |
39 | 40 | private val pageMarginState = mutableStateOf(0) |
40 | 41 | private val offscreenPageLimitState = mutableStateOf(0) |
| 42 | + private val sameOrientationChildGestureState = mutableStateOf(false) |
41 | 43 | private var initialPage = 0 |
42 | 44 | private var didEmitInitialPage = false |
43 | 45 | private var currentPage = 0 |
@@ -72,13 +74,23 @@ class ComposePagerView(context: Context) : FrameLayout(context) { |
72 | 74 | } |
73 | 75 |
|
74 | 76 | override fun onDetachedFromWindow() { |
| 77 | + updateSameOrientationAncestorsGestureState(false) |
75 | 78 | if (composeView.parent === this) { |
76 | 79 | super.removeView(composeView) |
77 | 80 | didSetContent = false |
78 | 81 | } |
79 | 82 | super.onDetachedFromWindow() |
80 | 83 | } |
81 | 84 |
|
| 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 | + |
82 | 94 | override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { |
83 | 95 | super.onMeasure(widthMeasureSpec, heightMeasureSpec) |
84 | 96 | measureComposeView() |
@@ -186,6 +198,21 @@ class ComposePagerView(context: Context) : FrameLayout(context) { |
186 | 198 | pageMarginState.value = value |
187 | 199 | } |
188 | 200 |
|
| 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 | + |
189 | 216 | fun setCurrentItem(selectedPage: Int, animated: Boolean) { |
190 | 217 | if (selectedPage < 0 || pages.isNotEmpty() && selectedPage >= pages.size) { |
191 | 218 | return |
@@ -310,7 +337,7 @@ class ComposePagerView(context: Context) : FrameLayout(context) { |
310 | 337 | val reverseLayout = layoutDirectionState.value == LayoutDirection.Rtl |
311 | 338 | val pageSpacing = pageMarginState.value.dp |
312 | 339 | val beyondViewportPageCount = offscreenPageLimitState.value |
313 | | - val userScrollEnabled = scrollEnabledState.value |
| 340 | + val userScrollEnabled = scrollEnabledState.value && !sameOrientationChildGestureState.value |
314 | 341 |
|
315 | 342 | if (orientationState.value == Orientation.Vertical) { |
316 | 343 | VerticalPager( |
|
0 commit comments