Android - ViewPager in ScrollView

One of the most famous question about Android is how can we put a ViewPager into the ScrollView. Yes i know, put a scrollable into an another scrollable is not a good idea in Android. And Google says never do that. But, what can we do? For example iOS guys can do that very easily and when your customer see this property in iOS app, wants the same property in Android app. Ok, let's see what can we do about that?

The important think is scroll gesture. When we scroll vertically or horizontally, viewpager never knows what is happening. Because, scroll gestures are handled by scrollview. So, if we can steal scroll event from the scrollview, we can inform viewpager about scrolling.

To steal scroll event from the scrollview, we have to create our custom scrollview and create our gesture detector. And in our gesture detector, we have to check gesture direction. If gesture direction is vertical, release gesture to be handled by scrollview. If gesture direction is horizontal, prevent the event to be handled by viewpager.

public class CustomScrollView extends ScrollView {

    private GestureDetector mGestureDetector;

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mGestureDetector = new GestureDetector(context, new YScrollDetector());
        setFadingEdgeLength(0);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return super.onInterceptTouchEvent(ev)
               && mGestureDetector.onTouchEvent(ev);
    }

    // Return false if we're scrolling in the x direction
    class YScrollDetector extends SimpleOnGestureListener {
        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2,
                                float distanceX, float distanceY) {
            return (Math.abs(distanceY) > Math.abs(distanceX));
    }
}

That's all. Put your viewpager into this custom scrollview in layout.
<com.muratonnet.sample.widget.CustomScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        ...

        <android.support.v4.view.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="250dp" />

        ...

    </LinearLayout>

</com.muratonnet.sample.widget.CustomScrollView>

You can find source code and sample usage on

13 comments:

Anonymous said...

Hello Mr.Murat,
I want to thank you for your great tip, that help me a lot thank to you.
Bye

Unknown said...

Not Working any more..........

Anonymous said...

Not working.

Unknown said...

Hi.
How can we make this code work for "ScrollView inside a page of ViewPager"?

Anonymous said...

Your Tutorial is too good but how can i set dynamic height if viewpager have greater height then screen size?

nilesh said...

it is not working in bellow lolipop.

Anonymous said...

Thanks for your great and helpful presentation I like your good service.I always appreciate your post.That is very interesting I love reading and I am always searching for informative information like this.Well written article Thank You for Sharing with Us pmp training Chennai | pmp training centers in Chenai | pmp training institutes in Chennai | pmp training and certification in Chennai | pmp training in velachery

tejaswini said...

stunning, incredible, I was thinking about how to fix skin inflammation normally.I've bookmark your site and furthermore include rss. keep us refreshed.
data scientist malaysia

360digitmgdelhi said...

Simply the manner in which I have anticipated. Your site truly is intriguing.
iot courses in delhi

dataanalyticscourse said...

Here at this site actually the particular material assortment with the goal that everyone can appreciate a great deal.
360DigiTMG big data course in malaysia

tejaswini said...

I truly like your style of blogging. I added it to my preferred's blog webpage list and will return soon…
masters in artificial intelligence

Anonymous said...

This is a very nice one and gives in-depth information. I am really happy with the quality and presentation of the article.
JAVA classes in Akola

traininginstitute said...

I am impressed by the information that you have on this blog. It shows how well you understand this subject.
data scientist training in malaysia

Post a Comment