Android - 在运行时更改ViewPager的高度

时间:2014-08-29 23:11:33

标签: android android-viewpager height

我想更改ViewPager的高度,因为它包含一些不同高度的子项。我已经读过,对于ViewPager来说,wrap_content是不可能的,所以我想在运行时更改它的高度。 我试过这个但是没有用:

pager.getLayoutParams().height=1000;

有人可以提供帮助吗?

CustomViewPager.class

public class CustomViewPager extends ViewPager {

/**
 * Constructor
 * 
 * @param context
 *            the context
 */
public CustomViewPager(Context context) {
    super(context);
}

/**
 * Constructor
 * 
 * @param context
 *            the context
 * @param attrs
 *            the attribute set
 */
public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);

    int height = 0;
    for (int i = 0; i < getChildCount(); i++) {

        View child = getChildAt(i);

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

}

xml文件:

<com.arsoft.ex.viewpager.CustomViewPager
                android:id="@+id/viewPager"
                android:layout_width="match_parent"
                android:layout_height="wrap_content" >
            </com.arsoft.ex.viewpager.CustomViewPager>

MainClass:

tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
    pager = (CustomViewPager) findViewById(R.id.viewPager);
    adapter = new MyPagerAdapter(getSupportFragmentManager());

    pager.setAdapter(adapter);

    final int pageMargin = (int) TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
                    .getDisplayMetrics());
    pager.setPageMargin(pageMargin);

    tabs.setViewPager(pager);

FragmentPagerAdapter:

    public class MyPagerAdapter extends FragmentPagerAdapter {

    private final String[] TITLES = { "Cook", "Recipe", "Comments" };

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

    @Override
    public int getCount() {
        return TITLES.length;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position) {
        case 0: **pager.getLayoutParams().height=100;**
            return CookCardFragment.newInstance(position, pager);

        case 1: **pager.getLayoutParams().height=400;**
            return RecipeCardFragment.newInstance(position, pager);

        case 2: **pager.getLayoutParams().height=1000;**
            return CookCardFragment.newInstance(position, pager);
        }
        return null;
    }

}

2 个答案:

答案 0 :(得分:3)

我以前见过这个问题。 也许你可以使用这样的功能:

 ViewPager vp = (CustomViewPager) findViewById(R.id.viewPager);
 ...
 ViewGroup.LayoutParams params = vp.getLayoutParams();
 params.height = 1000;
 vp.setLayoutParams(params);

答案 1 :(得分:0)

检查我的回答,我可以在更改页面或更改其高度时更改ViewPager的高度

https://stackoverflow.com/a/25710358/1835245