22

I need to have a ViewPager inside a ScrollView but ViewPager just does not appear when it's in ScrollView, everything is ok when i don't use ScrollView.

I've seen a couple questions like this being asked here on stackoverflow or on other sites and all of them have been answered that you have to put android:fillViewport="true" to your scrollview to fix the problem, but this solution doesnt work for me, ViewPager still does not appear even if i have android:fillViewport="true" in my ScrollView.

I guess something got changed in the android api's or something and this solution doesn't work anymore, does anyone know how i could possibly make a ViewPager appear in a ScrollView?

UPDATE: A working ScrollView layout XML:

<ScrollView
    android:id="@+id/scrollView1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true">


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical">

            <android.support.v4.view.ViewPager
                android:id="@+id/itemsViewPager2"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </android.support.v4.view.ViewPager>
    </LinearLayout>
 </ScrollView>
Lavekush Agrawal
  • 6,040
  • 7
  • 52
  • 85
boogieman
  • 567
  • 2
  • 5
  • 10
  • 1
    You should provide more info about your problem. The layout you're currently trying to get this to work for example. Have you tried defining an exact pixel value for the height of the ViewPager? – Zsombor Erdődy-Nagy Jan 27 '12 at 15:02
  • Wow, thanks for the idea, i played a bit with height and width attributes and actually made it to work pretty easily, updated original question with scrollview XML layout that works. – boogieman Jan 27 '12 at 15:25
  • @boogieman please put in as answer and mark it so. – Warpzit Oct 12 '12 at 07:50
  • You can find the solution [here][1] [1]: http://stackoverflow.com/questions/2646028/android-horizontalscrollview-within-scrollview-touch-handling – Gaurav Arora Nov 26 '12 at 05:12

4 Answers4

17

I ran into a similar problem in the past. A View-Pager must have a set height (it cannot wrap-content). Since a ViewPager loads separate pages, and not all at once, it won't know what 'wrap-content' actually means. Setting the layout_height to fill_parent or a set dp allows the ViewPager to statically set it's height and react more accordingly within other layouts.

jbenowitz
  • 1,795
  • 6
  • 26
  • 39
9
public class YourScrollableViewPager extends ViewPager {

private static final int MATCH_PARENT = 1073742592;

private int currentPageNumber;
private int pageCount;

public YourScrollableViewPager(Context context) {
    super(context);
    prepareUI();
}

public YourScrollableViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    prepareUI();
}

private void prepareUI() {
    setOffscreenPageLimit(pageCount);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int height = 0;
    if (getChildCount() != 0) {
        View child = getChildAt(currentPageNumber);
        child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        int h = child.getMeasuredHeight();
        if (h > height) height = h;
    }
    heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}

public void onPrevPage() {
    onMeasure(MATCH_PARENT, 0);
}

public void onNextPage() {
    onMeasure(MATCH_PARENT, 0);
}}
Andrew Dmytrenko
  • 380
  • 3
  • 11
  • Thanks, it works for me. But I faced with some issue. I have 2 fragments at view pager. One have NestedScrollView and the other one have RecyclerView. So with that code fling is broken at RecyclerView. – Andrew Oct 08 '15 at 12:44
2

I found solution for mine using "HorizontalScrollView" instead of ScrollView.

First create activity_main.xml with below code, where you define ViewPager.

        <?xml version="1.0" encoding="utf-8"?>
           <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               android:orientation="vertical" >

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

And later create another left.xml for defining your HorizontalScrollView :

        <?xml version="1.0" encoding="utf-8"?>
        <HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res
              /android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:orientation="vertical" >

           <ImageView
              android:id="@+id/imageView1"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:src="@drawable/ic_launcher" />
        </HorizontalScrollView>
Nibha Jain
  • 7,742
  • 11
  • 47
  • 71
TANZ
  • 56
  • 5
-2

Just add the android:fillViewport="true" to your scroll view it will work

Sujithrao
  • 789
  • 3
  • 12
  • 27