package com.zztrapp.zztrapp.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.widget.GridView;

import com.zztrapp.zztrapp.R;

/**
 * Describe MaxHeightGridView
 * Created by Bby on 2022/6/26
 * Email bby0856@163.com
 */
public class MaxHeightGridView extends GridView {

    private int mHeightMax;

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

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

    public MaxHeightGridView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initialize(context, attrs);
    }

    private void initialize(Context context, AttributeSet attrs) {
        TypedArray arr = context.obtainStyledAttributes(attrs, R.styleable.maxHeight_RecyclerView);
        mHeightMax = arr.getLayoutDimension(R.styleable.maxHeight_RecyclerView_maxHeight, mHeightMax);
        arr.recycle();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        if (mHeightMax > 0) {
            heightMeasureSpec = MeasureSpec.makeMeasureSpec(mHeightMax, MeasureSpec.AT_MOST);
        }
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

}
