privatefunhandleInterceptTouchEvent(e: MotionEvent) { val orientation = parentViewPager?.orientation ?: return
// Early return if child can't scroll in same direction as parent if (!canChildScroll(orientation, -1f) && !canChildScroll(orientation, 1f)) { return }
if (e.action == MotionEvent.ACTION_DOWN) { initialX = e.x initialY = e.y Log.e("request", "true 1") parent.requestDisallowInterceptTouchEvent(true) } elseif (e.action == MotionEvent.ACTION_MOVE) { val dx = e.x - initialX val dy = e.y - initialY val isVpHorizontal = orientation == ORIENTATION_HORIZONTAL
// assuming ViewPager2 touch-slop is 2x touch-slop of child val scaledDx = dx.absoluteValue * if (isVpHorizontal) .5felse1f val scaledDy = dy.absoluteValue * if (isVpHorizontal) 1felse.5f
if (scaledDx > touchSlop || scaledDy > touchSlop) { if (isVpHorizontal == (scaledDy > scaledDx)) { // Gesture is perpendicular, allow all parents to intercept Log.e("request", "false 1") parent.requestDisallowInterceptTouchEvent(false) } else { // Gesture is parallel, query child if movement in that direction is possible if (canChildScroll(orientation, if (isVpHorizontal) dx else dy)) { // Child can scroll, disallow all parents to intercept Log.e("request", "true 2") parent.requestDisallowInterceptTouchEvent(true) } else { Log.e("request", "false 2") // Child cannot scroll, allow all parents to intercept parent.requestDisallowInterceptTouchEvent(false) } } } } }