在Android中,ListView, GridView是很好容器,用于规则展现条目,而且在超出范围时能够上下滑动展现更多。通常的,都是拿来单独使用。 有时候,咱们须要把他们放在ScrollView中,不须要他们的滑动效果,仅仅用做展现效果,可是这个时候ListView,GridView每每展现不彻底。html
####解决办法android
GridViewless
public class GridViewInScrollView extends GridView { public GridViewInScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public GridViewInScrollView(Context context) { super(context); } public GridViewInScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
ListViewide
public class ListViewInScrollView extends ListView { public ListViewInScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ListViewInScrollView(Context context) { super(context); } public ListViewInScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST); super.onMeasure(widthMeasureSpec, expandSpec); } }
####分析ui
关键的代码就是复写了OnMeasure方法,在这个方法里面利用MeasureSpec构建一个测量高度。先解释下MeasureSpec这个类:code
A MeasureSpec encapsulates the layout requirements passed from parent to child. Each MeasureSpec represents a requirement for either the width or the height. A MeasureSpec is comprised of a size and a mode. There are three possible modes:htm
UNSPECIFIED The parent has not imposed any constraint on the child. It can be whatever size it wants. EXACTLY The parent has determined an exact size for the child. The child is going to be given those bounds regardless of how big it wants to be. AT_MOST The child can be as large as it wants up to the specified size. MeasureSpecs are implemented as ints to reduce object allocation. This class is provided to pack and unpack the <size, mode> tuple into the int.three
MeasureSpec是一个帮助类,帮助记录测量的属性和数值。一个int的测量值能够分红两个部分,后16位标识模式,前16位标识大小。三种模式在上面都有解释。在ListView的源码中能够看到,在onMeasure方法中对AT_MOST模式作了特殊处理,调用了measureHeightOfChildren方法测量显示高度。这个方法中计划了每个view和高度和divider的高度以及ListView的padding,最终累加返回一个准确的高度。GridView也是同样的道理。ci