2

I have a ViewPager and in its adapter I have 3 childs(Fragments). These 3 childs should have different heights measured on content.

I have already tried to override the onMeasure Methode in ViewPager like this Android: I am unable to have ViewPager WRAP_CONTENT

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

}

But this don't work for all the childs in the ViewPager it only sets one height for all.

This is the CustomViewPager extended from ViewPager:

This is the adapter for 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;
    }

}
Kaushik
  • 6,150
  • 5
  • 39
  • 54
ninjaxelite
  • 1,139
  • 2
  • 20
  • 43

2 Answers2

1
@Override
    public float getPageWidth(int position) {
    return 0.98f; //(0-1] this values specifies how much space a page will occupy on screen
}
KiDa
  • 233
  • 1
  • 3
  • 10
0

Do one thing just try to change the height of viewpager control every time you call new fragment in your FragmentActivity

sandy
  • 203
  • 1
  • 7
  • The problem is that the childs of the viewPager haven't always the same height. For instance I have a comments child and the viewPager should scale its height deppending on content. – ninjaxelite Aug 28 '14 at 07:41
  • change viewpager height to android:layout_height="wrap_content" – sandy Aug 28 '14 at 09:57