/*
 * Copyright (c) 2015. Ncomz. All Rights Reserved
 */

package com.ntels.kneet.v3.common.widget;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.View;

public class CustomViewPager extends ViewPager {

    private View mCurrentView;

    public CustomViewPager(Context context) {

        super(context);
    }

    public CustomViewPager(Context context, AttributeSet attrs) {

        super(context, attrs);
    }

    @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

        if ( mCurrentView == null ) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            return;
        }
        int height = 0;
        mCurrentView.measure(widthMeasureSpec, View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
        int h = mCurrentView.getMeasuredHeight();
        if ( h > height )
            height = h;
        heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(height, View.MeasureSpec.EXACTLY);

        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void measureCurrentView(View currentView) {

        mCurrentView = currentView;
        requestLayout();
    }

    public int measureFragment(View view) {

        if ( view == null )
            return 0;

        view.measure(0, 0);
        return view.getMeasuredHeight();
    }
}