Prevent Swiping of ViewPager2 when onTouch of inner view

前端 未结 1 1118
情话喂你
情话喂你 2020-12-20 03:31

I have a ViewPager2 with a fragment inside,

in the fragment, I have a custom view with certain touch logic that involves moving the finger.

how do I prevent

相关标签:
1条回答
  • 2020-12-20 04:18

    Set OnTouchListener for inner view. In onTouch() method, call:

    viewPager.requestDisallowInterceptTouchEvent(true)
    

    ViewPager handles its swiping motion in onInterceptTouchEvent(). Above code prevents ViewPager from calling onInterceptTouchEvent(). When you're swiping, ViewPager returns true in onInterceptTouchEvent() which also prevents touch events to be passed to child views. Therefore disallowing intercept allows child views to handle touch events.

    Set back to false when the inner view is not being touched.

    From my experience, onInterceptTouchEvent() prevents onTouchEvent(). It does not prevent OnTouchListener. So the key here is to set up OnTouchListener for the inner view.

    0 讨论(0)
提交回复
热议问题