0

I have three fragments on the viewpager and all three have recycler view on them. But while swiping recycler view's scroll conflicts with it. Hence it is only after 3rd or 4th swipe the fragment is swiped or sometimes I have to swipe from the start of the screen to the end then the fragment is swiped. I want a smooth swiping ux. Can someone please help me out with this issue. Thanks!

Tsar
  • 63
  • 2
  • 10

1 Answers1

0

The ViewGroup class has a method called onInterceptTouchEvent(ev: MotionEvent)which returns Boolean value. If you want your view to "steal" the touch event you should return true and false otherwise.

For example, if you want your recycler view to be scrolled only vertically you should override onInterceptTouchEvent(ev: MotionEvent) in your ViewPager class and detect scroll type.

It will look like this:

class MyViewPager {
    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean = 
       !isVerticalScroll()
}

Method isVerticalScroll() should be created by you and return current scroll direction.

So when you swipe horizontally you will get to the next fragment. At the same time your recycler view will be scrolled vertically and no conflicts will occur.

David
  • 351
  • 3
  • 11
  • my onInterceptTouchEvent is returning false no matter whatever i try – Tsar Jan 19 '19 at 14:55
  • i tried this https://stackoverflow.com/questions/7814017/is-it-possible-to-disable-scrolling-on-a-viewpager, but its still not working. – Tsar Jan 19 '19 at 15:04