dev
dev

Reputation: 11309

How to scroll ViewPager both horizontally and vertically in Android?

I have a ViewPager whose contents are determined runtime. The content size might be more than the screen dimensions, so I thought of having the ViewPager inside a NestedScrollView (which enables vertical scroll) which is inside a HorizontalScrollView (which enables horizontal scroll). This solves the problem of fitting the content, but I am unable to scroll through the ViewPager tabs - possibly ViewPager is not receiving the horizontal scrolls anymore.

For example, in a scenario when content width is more than screen width, my initial scroll should first scroll to the end of content's edges - and then the new content from the FragmentAdapter should be loaded. But with nested scrollview approach, I am unable to load next item from the FragmentAdapter.

My code :

<HorizontalScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.v4.view.ViewPager
                android:layout_width="match_parent"
                android:layout_height="wrap_content" />

        </RelativeLayout>

    </android.support.v4.widget.NestedScrollView>
</HorizontalScrollView>

There is one alternative to this approach. Drop the outer HorizontalScrollView and put the content of ViewPager's FragmentAdapter in a HorizontalScrollView. I am yet to try this solution - but this would lead me to change and retest all my existing layout files for the FragmentAdapter.

Upvotes: 1

Views: 1694

Answers (1)

shekhar pande
shekhar pande

Reputation: 1228

You can use bellow references. Good Implementation.

DoubleViewPager

Upvotes: 1

Related Questions