Skip to content

Instantly share code, notes, and snippets.

@ercnksgl
Created November 20, 2022 18:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ercnksgl/d427f00eacadcc6e233b1baab89fc8b1 to your computer and use it in GitHub Desktop.
Save ercnksgl/d427f00eacadcc6e233b1baab89fc8b1 to your computer and use it in GitHub Desktop.
ViewPager inside RecyclerView Scroll issue
package com.ercnksgl.testapp.util
import android.view.MotionEvent
import androidx.recyclerview.widget.RecyclerView
import kotlin.math.abs
class DisallowParentSwipeOnItemTouchListener : RecyclerView.OnItemTouchListener {
var startPoint = 0f
override fun onInterceptTouchEvent(
rv: RecyclerView,
e: MotionEvent
): Boolean {
when (e.action) {
MotionEvent.ACTION_DOWN -> {
startPoint = e.x
}
MotionEvent.ACTION_MOVE -> {
val pointX = e.x
if (abs(pointX - startPoint) > 5f) {
//scrolling horizontally
rv.parent.requestDisallowInterceptTouchEvent(true)
}
}
MotionEvent.ACTION_UP,
MotionEvent.ACTION_CANCEL -> {
rv.parent.requestDisallowInterceptTouchEvent(false)
}
}
return false
}
override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {}
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment