4

I have a ViewPager implemented with Views only. Each page has 4 elements, Title(TextView), SubTilte(TextView), Description(TextView) and ListView. There are Title and list in each page but other elements will not there for always. I.e., 1st page has title ,subtitle and list, second has title and list and third page has title, subtitle, description and list. So the third page has big height compared to other pages. I want to have a same size for each pages of viewpager. I have a custom viewpager class and it is working but only after I scrolled all the pages but for the first time it is not working :-

public class WrapContentViewPager extends ViewPager {

    private int mCurrentPagePosition = 0;

    public WrapContentViewPager(Context context) {
        super(context);
    }

    public WrapContentViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        try {
            View child = getChildAt(mCurrentPagePosition);
            if (child != null) {
                child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                int h = child.getMeasuredHeight();
                heightMeasureSpec = MeasureSpec.makeMeasureSpec(h, MeasureSpec.EXACTLY);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

Here is my xml layout :-

<RelativeLayout
    android:id="@+id/showCardsLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">

    <co.mobile_android.customViews.WrapContentViewPager
        android:id="@+id/cardsList"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true" />

</RelativeLayout>

Please help :)

Mahesh Babariya
  • 4,560
  • 6
  • 39
  • 54
Sid
  • 199
  • 1
  • 12
  • Follow below link: It will help you: http://stackoverflow.com/questions/25674124/viewpager-with-expandable-child/25710358#25710358 – Mavya Soni Dec 10 '16 at 09:34

0 Answers0