7

Does the ViewPager have to be the only object present inside the activity layout? I'm trying to implement something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/reader_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

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

<Gallery
    android:id="@+id/miniatures_gallery"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" /></LinearLayout>

Where should I have a big pager scrolling in the top (and I have it) and a smaller gallery scrolling under that. This shows me only the pager and not the gallery. Any suggestion?

midhunhk
  • 5,560
  • 7
  • 52
  • 83
Lorenzo Sciuto
  • 1,655
  • 3
  • 27
  • 57

3 Answers3

6

The ViewPager does not support wrap_content as it (usually) never have all its children loaded at the same time, and can therefore not get an appropriate size (the option would be to have a pager that changes size every time you have switched page).

You can however set a precise dimension (e.g. 150dp) and match_parent works as well.
You can also modify the dimensions dynamically from your code by changing the height-attribute in its LayoutParams.

Jave
  • 31,598
  • 14
  • 77
  • 90
  • 1
    What is it that doesn't work with it? Another solution, that works if you want the pager to fill any empty space is this: Then the pager leaves just enough space for any other views. – Jave Dec 16 '11 at 10:13
5

Assign layout weight to view pager as 1 & height = 0dp instead of wrap_content

<android.support.v4.view.ViewPager
    android:id="@+id/page_viewer"
    android:layout_width="match_parent"
    android:layout_height="0dp" 
    android:layout_weight="1"/>
default locale
  • 13,035
  • 13
  • 56
  • 62
napster
  • 687
  • 1
  • 8
  • 18
0

I solved the problem with a couple of hacks. Here is what it involves:

First, I needed a layout that would ignore ViewPager's height limit. Used it as a parent layout for ViewPager items.

public class TallLinearLayout extends LinearLayout {

    public TallLinearLayout(Context context) {
        super(context);
    }

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

    public TallLinearLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, MeasureSpec.UNSPECIFIED);
    }
}

I then wrote the logic for resizing ViewPager:

private class ViewPagerContentWrapper implements OnGlobalLayoutListener {
    private ViewPager mViewPager;

    public ViewPagerContentWrapper(ViewPager viewPager) {
        mViewPager = viewPager;
    }

    @Override
    public void onGlobalLayout() {
        int position = mViewPager.getCurrentItem(); 
        check(position);
        check(position + 1);
    }

    private void check(int position) {
        ViewGroup vg = (ViewGroup) mViewPager.getChildAt(position); 
        View v = vg == null ? null : vg.getChildAt(0);

        if (v != null) {
            int height = v.getHeight();
            if (height > mViewPager.getHeight()) {
                resize(height);
            }
        }
    }

    private void resize(int height) {
        mViewPager.setLayoutParams(
                new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        height
                )
        );
    }
}

Which I registered as global layout listener:

viewPager.getViewTreeObserver().addOnGlobalLayoutListener(new ViewPagerContentWrapper(viewPager));
midhunhk
  • 5,560
  • 7
  • 52
  • 83
Pijusn
  • 11,025
  • 7
  • 57
  • 76
  • This will create an infinite layout cycle. Initially system requests onLayout that will call your OnGlobalLayoutListener that will call onLayout that will call OnGlobalLayoutListener and so on... – Dmitriy Tarasov Feb 20 '14 at 14:14