package com.lean56.andplug.view; import android.annotation.TargetApi; import android.content.Context; import android.os.Build; import android.util.AttributeSet; import android.view.ViewGroup; import android.widget.GridView; /** * GridView with auto height * * @author Charles <zhangchaoxu@gmail.com> */ public class AutoHeightGridView extends GridView { public AutoHeightGridView(Context context) { super(context); } public AutoHeightGridView(Context context, AttributeSet attrs) { super(context, attrs); } public AutoHeightGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) public AutoHeightGridView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { ViewGroup.LayoutParams layoutParams = getLayoutParams(); if(null != layoutParams && layoutParams.height == ViewGroup.LayoutParams.WRAP_CONTENT) { // make GridView support WRAP_CONTENT height // see {http://www.jayway.com/2012/10/04/how-to-make-the-height-of-a-gridview-wrap-its-content/} heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }