Android - Like a ViewPager with multiple children with different heights

I have a ViewPager and in its adapter I have 3 children (fragments). These 3 children should have different heights, measured in content.

I already tried to override the onMeasure method in ViewPager as follows: Android: I don't 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 does not work for all descendants in the ViewPager, it sets only one height for all.

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

}
+1
source share
2 answers

, viewpager , FragmentActivity

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

All Articles