因为嵌套的ListView不需要也不应该能滚动

所以在代码中 需要将嵌套的ListView调整到刚好能显示所有Child的高度

而对于ListView本身而言,它是不关心自身高度和是否能显示下所有Child的,因为就原始设计上而言,它是一个可以滚动的组件.

你也可以自己阅读下面这段代码来看看ListView的measure自身高度的逻辑

看看MeasureSpec.UNSPECIFIED模式下ListView的高度是怎么来的

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// Sets up mListPadding

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int widthMode = MeasureSpec.getMode(widthMeasureSpec);

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);

measureScrapChild(child, 0, widthMeasureSpec);

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);

}

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;

}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐