We used SwipeRefreshLayout in the Fragment in ViewPager2. SwipeRefreshLayout is stuck on the page as SwipeRefreshLayout moves even slightly to the left or right when SwipeRefreshLayout is swiped down.

  • Solution 1: Rewrite SwipeRefreshLayout

We’re going to modify ViewPager2 first, but it doesn’t inherit. Look to SwipeRefreshLayout.

class MySwipeRefreshLayout : SwipeRefreshLayout { constructor(context: Context, attes: AttributeSet) : super(context, attes) {} constructor(context: Context) : Super (context) private var startX = 0 private var beginScrolll = false Int = 0 override fun dispatchTouchEvent(ev: MotionEvent): Boolean { when (ev.action) { MotionEvent.ACTION_DOWN -> { startX = ev.x.toInt() startY = ev.y.toInt() parent.requestDisallowInterceptTouchEvent(true) } MotionEvent.ACTION_MOVE -> { val endX = ev.x.toInt() val endY = ev.y.toInt() val disX = Math.abs(endX - startX) val disY: Int = Math.abs(endY - startY) if (disX > disY) { if (! beginScrolll) parent.requestDisallowInterceptTouchEvent(false) } else { beginScrolll = true parent.requestDisallowInterceptTouchEvent(true) } } MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { parent.requestDisallowInterceptTouchEvent(false) beginScrolll=false } } return super.dispatchTouchEvent(ev) } }Copy the code
  • Solution 2: Use SmartRefreshLayout instead of SwipeRefreshLayout.

Address of reference text