Android ViewPager dimension

雨燕双飞 提交于 2019-12-18 16:45:33

问题


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?


回答1:


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.




回答2:


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"/>



回答3:


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));


来源:https://stackoverflow.com/questions/8532307/android-viewpager-dimension

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!