Reputation: 1198
I want to change the height of a ViewPager because it contains some childs with different height. I have read that wrap_content is not possible for ViewPager so I want to change the height of it in runtime. I have tried this but it is not working:
pager.getLayoutParams().height=1000;
Could someone offer help please?
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 file:
<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;
}
}
Upvotes: 3
Views: 10026
Reputation: 151
Check my answer, I was able to change the height of ViewPager when you change the page or change the height of it child
https://stackoverflow.com/a/25710358/1835245
Upvotes: 0
Reputation: 265
I have seen this problem before. Maybe you could use the fumction like this:
ViewPager vp = (CustomViewPager) findViewById(R.id.viewPager);
...
ViewGroup.LayoutParams params = vp.getLayoutParams();
params.height = 1000;
vp.setLayoutParams(params);
Upvotes: 3