mirror of
https://github.com/divkit/divkit.git
synced 2026-06-06 20:07:59 +00:00
Fix restrict_parent_scroll
e11db1fcce42b0fc3bbe4261cbc9c51603e4f703
This commit is contained in:
@@ -178,7 +178,7 @@ internal class DivPagerBinder @Inject constructor(
|
||||
|
||||
view.addSubscription(div.restrictParentScroll.observeAndGet(resolver) { restrictParentScroll ->
|
||||
view.onInterceptTouchEventListener = if (restrictParentScroll) {
|
||||
ParentScrollRestrictor(ParentScrollRestrictor.DIRECTION_HORIZONTAL)
|
||||
ParentScrollRestrictor
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
+1
-11
@@ -172,9 +172,7 @@ internal class DivGalleryBinder @Inject constructor(
|
||||
}
|
||||
view.addOnScrollListener(ScrollListener(context, view, itemHelper, div))
|
||||
view.onInterceptTouchEventListener = if (div.restrictParentScroll.evaluate(resolver)) {
|
||||
ParentScrollRestrictor(
|
||||
divOrientation.toRestrictorDirection()
|
||||
)
|
||||
ParentScrollRestrictor
|
||||
} else {
|
||||
null
|
||||
}
|
||||
@@ -368,12 +366,4 @@ internal class DivGalleryBinder @Inject constructor(
|
||||
divBinder.bind(context, divView, div, path)
|
||||
}
|
||||
}
|
||||
|
||||
@ParentScrollRestrictor.Direction
|
||||
private fun DivGallery.Orientation.toRestrictorDirection(): Int {
|
||||
return when (this) {
|
||||
DivGallery.Orientation.HORIZONTAL -> ParentScrollRestrictor.DIRECTION_HORIZONTAL
|
||||
DivGallery.Orientation.VERTICAL -> ParentScrollRestrictor.DIRECTION_VERTICAL
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+1
-1
@@ -111,7 +111,7 @@ internal class DivTabsBinder @Inject constructor(
|
||||
|
||||
view.addSubscription(div.restrictParentScroll.observeAndGet(resolver) { restrictScroll ->
|
||||
view.viewPager.onInterceptTouchEventListener = if (restrictScroll) {
|
||||
ParentScrollRestrictor(ParentScrollRestrictor.DIRECTION_HORIZONTAL)
|
||||
ParentScrollRestrictor
|
||||
} else {
|
||||
null
|
||||
}
|
||||
|
||||
+1
-52
@@ -1,73 +1,22 @@
|
||||
package com.yandex.div.core.view2.divs.widgets
|
||||
|
||||
import android.view.MotionEvent
|
||||
import android.view.ViewConfiguration
|
||||
import android.view.ViewGroup
|
||||
import androidx.annotation.IntDef
|
||||
import com.yandex.div.internal.widget.OnInterceptTouchEventListener
|
||||
import kotlin.math.abs
|
||||
|
||||
internal class ParentScrollRestrictor(
|
||||
@Direction private val restrictedDirection: Int
|
||||
) : OnInterceptTouchEventListener {
|
||||
|
||||
private var touchSlop = UNDEFINED_TOUCH_SLOP
|
||||
private var initialTouchX = 0.0f
|
||||
private var initialTouchY = 0.0f
|
||||
@Direction private var scrollDirection = DIRECTION_NONE
|
||||
|
||||
internal object ParentScrollRestrictor: OnInterceptTouchEventListener {
|
||||
override fun onInterceptTouchEvent(target: ViewGroup, event: MotionEvent): Boolean {
|
||||
val parent = target.parent ?: return false
|
||||
|
||||
if (touchSlop == UNDEFINED_TOUCH_SLOP) {
|
||||
touchSlop = ViewConfiguration.get(target.context).scaledTouchSlop
|
||||
}
|
||||
|
||||
when (event.actionMasked) {
|
||||
MotionEvent.ACTION_DOWN -> {
|
||||
initialTouchX = event.x
|
||||
initialTouchY = event.y
|
||||
scrollDirection = DIRECTION_NONE
|
||||
|
||||
parent.requestDisallowInterceptTouchEvent(true)
|
||||
}
|
||||
|
||||
MotionEvent.ACTION_MOVE -> {
|
||||
if (scrollDirection == DIRECTION_NONE) {
|
||||
scrollDirection = findScrollDirection(event)
|
||||
}
|
||||
if (scrollDirection == restrictedDirection) {
|
||||
parent.requestDisallowInterceptTouchEvent(false)
|
||||
}
|
||||
}
|
||||
|
||||
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
|
||||
parent.requestDisallowInterceptTouchEvent(false)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
@Direction
|
||||
private fun findScrollDirection(event: MotionEvent): Int {
|
||||
val dx = abs(initialTouchX - event.x)
|
||||
val dy = abs(initialTouchY - event.y)
|
||||
|
||||
if (dx < touchSlop && dy < touchSlop) {
|
||||
return DIRECTION_NONE
|
||||
}
|
||||
|
||||
return if (dx > dy) DIRECTION_HORIZONTAL else DIRECTION_VERTICAL
|
||||
}
|
||||
|
||||
@IntDef(flag = true, value = [DIRECTION_NONE, DIRECTION_VERTICAL, DIRECTION_HORIZONTAL])
|
||||
annotation class Direction
|
||||
|
||||
companion object {
|
||||
const val DIRECTION_NONE = 0
|
||||
const val DIRECTION_HORIZONTAL = 1
|
||||
const val DIRECTION_VERTICAL = 1 shl 1
|
||||
|
||||
private const val UNDEFINED_TOUCH_SLOP = -1
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user