Android Dev: Android ViewPager inside a ScrollView

ViewPager scrolling downside.

I truly like Android ViewPager component. It can be used in so many ways: from app navigation to gallery like screens.

On the other hand, ViewPager is not very useful whether or not it’s nested right proper into a container with vertical scrolling. For example ViewPager within ScrollView or ListView with ViewPager as a listing merchandise view.
Say for instance: you drag the ViewPager‘s internet web page to view the next one, and aghhh the horizontal scrolling is disrupted and parent vertical scrolling begins.

Why is it that this happens?? The problem is when intercepting contact events from ViewPager by way of a parent container. The parent scroll container listens for scroll contact events and if there vertical shift, it takes the the event away from the child views. It basically thinks that particular user should scroll the parent container vertically.
So lets sort it out!

Android ViewPager inside a ScrollView

Android ViewPager inside a ScrollView

How to get it to work.
It doesn’t need any specific configuration from you. Merely use it as an alternative of the normal ViewPager and your users can also be comfortable with a simple and expected scrolling behavior!
So, how is it it done? There are ways to help.

  • Customized SimpleOnGestureListener subclass helps us to look at the first direction scrolling. If the X-axis shift higher than the Y-axis shift, it would inform ViewPager to call requestDisallowInterceptTouchEvent on the parent.
  • If the scroll gesture was once initiated horizontally then ViewPager will likely be “locked” unless gesture completion (finger up side). So the parent scroll container is not going to have the ability to intercept ViewPager‘s events. It’s fairly similar to iOS scroll view behavior which locks the scrolling axis when the scrolling directoin (vertical / horizontal) was determined.

Anyway just some Android dev exploring with the ViewPager within ScrollView.

Thanks for reading.