我有一个ViewPager,在其适配器中我有3个孩子(片段)。这三个孩子的内容应该有不同的高度。
我已经尝试像这样覆盖ViewPager中的onMeasure Methode(src:stackoverflow.com/questions/8394681/android-i-am-unable-to-have-viewpager-wrap-content/18167273#18167273)< / p>
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
View view = null;
for (int i = 0; i < getChildCount(); i++) {
// find the first child view
view = getChildAt(i);
if (view != null) {
// measure the first child view with the specified measure spec
view.measure(widthMeasureSpec, heightMeasureSpec);
}
setMeasuredDimension(getMeasuredWidth(),
measureHeight(heightMeasureSpec, view));
}
}
但是,这不适用于ViewPager中的所有孩子,它只为所有孩子设置了一个高度。
这是从ViewPager扩展的CustomViewPager:
这是viewPager的适配器:
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "card1", "card2", "Card3" };
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:
return CookCardFragment.newInstance(position);
case 1:
return RecipeCardFragment.newInstance(position);
case 2:
return CookCardFragment.newInstance(position);
}
return null;
}
}
答案 0 :(得分:0)
每次在FragmentActivity中调用新片段时,只需尝试更改viewpager控件的高度
答案 1 :(得分:0)
@Override
public float getPageWidth(int position) {
return 0.98f; //(0-1] this values specifies how much space a page will occupy on screen
}