Skip to content

Commit

Permalink
Refactor requestDisallowInterceptTouchEvent logic
Browse files Browse the repository at this point in the history
  • Loading branch information
vuongp committed Mar 16, 2021
1 parent c9d59d0 commit 6d6861d
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions library/src/main/java/nl/nos/imagin/ScrollHandler.kt
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,7 @@ class ScrollHandler(
distanceX: Float,
distanceY: Float
): Boolean {
val isScrollingVertically = isScrollingVertically(firstMotionEvent, moveMotionEvent)
if (!isScrollingVertically && imageView.rightEdgeIsVisible() && distanceX > 0 && moveMotionEvent?.pointerCount == 1) {
imageView.parent?.requestDisallowInterceptTouchEvent(false)
} else if (!isScrollingVertically && imageView.leftEdgeIsVisible() && distanceX < 0 && moveMotionEvent?.pointerCount == 1) {
if (shouldAllowIntercept(firstMotionEvent, moveMotionEvent, distanceX)){
imageView.parent?.requestDisallowInterceptTouchEvent(false)
}

Expand Down Expand Up @@ -71,6 +68,23 @@ class ScrollHandler(
}
})

/**
* Return whether the parent should be able to intercept touch events.
*/
private fun shouldAllowIntercept(
firstMotionEvent: MotionEvent?,
moveMotionEvent: MotionEvent?,
distanceX: Float
): Boolean {
if (isScrollingVertically(firstMotionEvent, moveMotionEvent)) return false
if (moveMotionEvent?.pointerCount != 1) return false

if (imageView.rightEdgeIsVisible() && distanceX > 0) return true
if (imageView.leftEdgeIsVisible() && distanceX < 0) return true

return false
}

private fun isScrollingVertically(
firstMotionEvent: MotionEvent?,
moveMotionEvent: MotionEvent?
Expand Down

0 comments on commit 6d6861d

Please sign in to comment.