For a new app that I’m working on, I wanted to use a ViewPager inside a ScrollView in order to make a simple image carousel. The only problem is, ScrollViews don’t seem to play nicely with horizontally swiping Views. As soon as the user moves their finger even slightly up or down, the ViewPager page change is cancelled, and the page snaps back to the current item. This can lead to an extremely frustrating user experience, because it is difficult for the user to understand why the views won’t swipe.
A popular solution that I’ve seen online is to use the ViewPager’s
getParent().requestDisallowInterceptTouchEvent(true); method to prevent the ScrollView from scrolling while the ViewPager is being touched or scrolled. The solution that I’ve been using is a bit like this, but it provides a little bit of a “drag threshold”, which is a bit forgiving in the event that the user is trying to drag the ScrollView, but happens to have their finger in the ViewPager. The code I used is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
I found this very useful solution on StackOverflow, among many other solutions that did work (although not as nicely as this one).