应用场景:在ListView的子项中嵌套GridView,需要重算每一个子项GridView的高度,否则GridView只能显示一行。
自定义Gridview:
import android.content.Context; import android.util.AttributeSet; import android.widget.GridView;
/** * 自定义GridView * <p> * Created by Karen Chia on 2019/4/1. * * @author Karen */ public class CustomGridView extends GridView {
public FloorHouseNumberGridView(Context context) { super(context); }
public FloorHouseNumberGridView(Context context, AttributeSet attrs) { super(context, attrs); }
public FloorHouseNumberGridView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }
/** * 设置不滚动(也就是让GridView的所有行都显示出来) */ @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
|