package com.kfzs.duanduan.view;

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

import java.util.HashMap;
import java.util.LinkedHashMap;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  ViewPagerAutoHeigh</p>
 * @ <p>Description:</p>
 * @ date:  2017/6/19 16:45
 * @ QQ:    315096953
 */

public class ViewPagerAutoHeigh extends ViewPager {

    private int current;
    private int[] mHeight = new int[10];
    /**
     * 保存position与对于的View
     */
    private HashMap<Integer, View> mChildrenViews = new LinkedHashMap<Integer, View>();
    private HashMap<Integer, Integer> mChildrenHeight = new LinkedHashMap<Integer, Integer>();

    private boolean scrollble = true;

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

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

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
            int h = child.getMeasuredHeight();
            mHeight[i] = h < 300 ? 300:h;
            Log.e("-----------child" + i, "minHeight:" + h);
        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(mHeight[getCurrentItem()], MeasureSpec.EXACTLY);
//        Log.e("-----------onMeasure", mChildrenHeight.get(current) + ",minHeight:" + mIntHeight);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void reSetHeight() {
        invalidate();
//        ViewGroup.LayoutParams layoutParams = getLayoutParams();
//        layoutParams.height = mHeight[getCurrentItem()];
//        setLayoutParams(layoutParams);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (!scrollble) {
            return true;
        }
        return super.onTouchEvent(ev);
    }


    public boolean isScrollble() {
        return scrollble;
    }

    public void setScrollble(boolean scrollble) {
        this.scrollble = scrollble;
    }
}
