package com.cooleshow.student.widgets;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

import com.cooleshow.student.adapter.HomeBottomPageAdapter;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;
import androidx.viewpager2.widget.ViewPager2;

/**
 * Author by pq, Date on 2022/8/31.
 */
public class CustomViewPager extends ViewPager {


    public CustomViewPager(@NonNull Context context) {
        super(context);
    }

    public CustomViewPager(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int height = 0;
        HomeBottomPageAdapter vpFragmentAdapter = (HomeBottomPageAdapter) getAdapter();
        if (vpFragmentAdapter != null) {
            View child = vpFragmentAdapter.getItem(getCurrentItem()).getView();
            if (child != null) {
                Log.i("pq", "viewpager=" + getCurrentItem());
                child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                height = child.getMeasuredHeight();
            }
        }
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }


    public void resetHeight() {
        requestLayout();
    }
}