Android ViewPager dimension

2020-02-28 10:16发布

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?

3条回答
霸刀☆藐视天下
2楼-- · 2020-02-28 10:40

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.

查看更多
够拽才男人
3楼-- · 2020-02-28 10:40

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));
查看更多
何必那么认真
4楼-- · 2020-02-28 11:00

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"/>
查看更多
登录 后发表回答