<ScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</ScrollView>
上面的布局在默认情况下,是达不到我们想到的效果,只会显示ListView的一行内容,同样的我们可以通过反推的方式,来查找原因的所在。
为何只会显示一行??我们可以先看看ListView的onMeasure方法
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Sets up mListPadding
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
int childWidth = 0;
int childHeight = 0;
int childState = 0;
mItemCount = mAdapter == null ? 0 : mAdapter.getCount();
if (mItemCount > 0 && (widthMode == MeasureSpec.UNSPECIFIED
|| heightMode == MeasureSpec.UNSPECIFIED)) {
final View child = obtainView(0, mIsScrap);
// Lay out child directly against the parent measure spec so that
// we can obtain exected minimum width and height.
measureScrapChild(child, 0, widthMeasureSpec, heightSize);
childWidth = child.getMeasuredWidth();
childHeight = child.getMeasuredHeight();
childState = combineMeasuredStates(childState, child.getMeasuredState());
if (recycleOnMeasure() && mRecycler.shouldRecycleViewType(
((LayoutParams) child.getLayoutParams()).viewType)) {
mRecycler.addScrapView(child, 0);
}
}
if (widthMode == MeasureSpec.UNSPECIFIED) {
widthSize = mListPadding.left + mListPadding.right + childWidth +
getVerticalScrollbarWidth();
} else {
widthSize |= (childState & MEASURED_STATE_MASK);
}
//当高度的测量模式为UNSPECIFIED,此时的ListView的高度就是一行的高度
if (heightMode == MeasureSpec.UNSPECIFIED) {
heightSize = mListPadding.top + mListPadding.bottom + childHeight +
getVerticalFadingEdgeLength() * 2;
}
if (heightMode == MeasureSpec.AT_MOST) {
// TODO: after first layout we should maybe start at the first visible position, not 0
heightSize = measureHeightOfChildren(widthMeasureSpec, 0, NO_POSITION, heightSize, -1);
}
setMeasuredDimension(widthSize, heightSize);
mWidthMeasureSpec = widthMeasureSpec;
}
从源码上可以看出,只有当高度的测量模式为UNSPECIFIED,此时的ListView的高度heightSize 就是一行的高度,所以我们要想办法让程序不进入if (heightMode == MeasureSpec.UNSPECIFIED)
这个语句内,而是进入if (heightMode == MeasureSpec.AT_MOST)
这个语句内,measureHeightOfChildren
方法才是测量统计所有的Item高度的实现
关于程序是如何进入if (heightMode == MeasureSpec.UNSPECIFIED)
内的,我们可以从ScrollView的onMeasure方法中得知
我们知道ViewGroup的measure测量过程是一个递归过程,它会在父元素中的onMeasure方法中,遍历所有的子元素进行对子元素逐个measure测量,而父元素的测量规格MeasureSpec同时也会影响到子元素的测量规格MeasureSpec;我们可以从ScrollView的onMeasure源码可以看出
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (!mFillViewport) {
return;
}
final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
if (heightMode == MeasureSpec.UNSPECIFIED) {
return;
}
……省略
}
我们进入super.onMeasure方法中,再去看看具体
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int count = getChildCount();
final boolean measureMatchParentChildren =
MeasureSpec.getMode(widthMeasureSpec) != MeasureSpec.EXACTLY ||
MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
mMatchParentChildren.clear();
int maxHeight = 0;
int maxWidth = 0;
int childState = 0;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
if (mMeasureAllChildren || child.getVisibility() != GONE) {
//这个方法很重要,是用测量子view的大小
measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
maxWidth = Math.max(maxWidth,
child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
maxHeight = Math.max(maxHeight,
child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
childState = combineMeasuredStates(childState, child.getMeasuredState());
if (measureMatchParentChildren) {
if (lp.width == LayoutParams.MATCH_PARENT ||
lp.height == LayoutParams.MATCH_PARENT) {
mMatchParentChildren.add(child);
}
}
}
}
……省略
}
从上面的源码可以看出,ScrollView的onMeasure方法内通过for循环遍历子view,通过measureChildWithMargins方法来实现子View的测量工作,我们再点进measureChildWithMargins方法内看看细节
protected void measureChildWithMargins(View child,
int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
final int childHeightMeasureSpec = getChildMeasureSpec(parentHeightMeasureSpec,
mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin
+ heightUsed, lp.height);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
从上面的代码可知,会根据父元素的parentWidthMeasureSpec、parentHeightMeasureSpec的测量规格,得到子元素的childWidthMeasureSpec 、childHeightMeasureSpec 测量规格,在其过程中并未改变子元素的测量模式,其实这时候我们看到measureChildWithMargins这个方法是ViewGroup的标准测量过程,而ScrollView已经对measureChildWithMargins方法进行重写了,接下来看看ScrollView的重写后的measureChildWithMargins方法
@Override
protected void measureChildWithMargins(View child, int parentWidthMeasureSpec, int widthUsed,
int parentHeightMeasureSpec, int heightUsed) {
final MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
final int childWidthMeasureSpec = getChildMeasureSpec(parentWidthMeasureSpec,
getPaddingLeft() + getPaddingRight() + lp.leftMargin + lp.rightMargin
+ widthUsed, lp.width);
final int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
lp.topMargin + lp.bottomMargin, MeasureSpec.UNSPECIFIED);
child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
}
从源码可知,在构建子元素的高度测量规格childHeightMeasureSpec 时,已经把子元素的测量模式设置成了UNSPECIFIED模式了,此时在回头看看ListView的onMeasure方法,就知道为何会进入if (heightMode == MeasureSpec.UNSPECIFIED)
内,接下来我们可以通过重写ListView的onMeasure方法,进行修改heightMeasureSpec的测量模式来解决只显示一行的问题,如下:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//问题是解决下,但缺点也是很明细,就是ListView的复用机制不起作用了
heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE << 2, MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
在makeMeasureSpec方法中,size未必就是Integer.MAX_VALUE << 2,只要测量值的范围内即可,越大越好。