Android ScrollView嵌套ListView显示不全问题

Android ScrollView嵌套ListView显示不全问题

未嵌套ScrollView
未嵌套ScrollView的ListView显示正常显示全
一旦嵌套了ScrollView
在这里插入图片描述
只显示了一行item的高度,显示不全
通过查看源码发现
因为ScrollView和ListView的heightMeasureSpec都是MeasureSpec.UNSPECIFIED

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // MeasureSpec有父级传进来,因为ScrollView为MeasureSpec.UNSPECIFIED所以这里的heightMode  == MeasureSpec.UNSPECIFIED
        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);
            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);
        }
		// 因为ListView的模式也是MeasureSpec.UNSPECIFIED,所以导致进入if
        if (heightMode == MeasureSpec.UNSPECIFIED) {
        // 高度就为一个的高度childHeight 
            heightSize = mListPadding.top + mListPadding.bottom + childHeight +
                    getVerticalFadingEdgeLength() * 2;
        }
        if (heightMode == MeasureSpec.AT_MOST) {
            heightSize = measureHeightOfChildren(widthMeasureSpec, 0, NO_POSITION, heightSize, -1);
        }

        setMeasuredDimension(widthSize, heightSize);

        mWidthMeasureSpec = widthMeasureSpec;
    }

所以导致显示不全是因是heightMode == MeasureSpec.UNSPECIFIED进入了

 if (heightMode == MeasureSpec.UNSPECIFIED) {
        // 高度就为一个的高度childHeight 
            heightSize = mListPadding.top + mListPadding.bottom + childHeight +
                    getVerticalFadingEdgeLength() * 2;
}

导致heightSize 被重新赋值为了一个childHeight 的高度

解决方法:

自己继承一个ListView重写onMeasure,通过MeasureSpec.makeMeasureSpec()方法来设置heightMode使他设置为MeasureSpec.AT_MOST

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // 将模式设置为MeasureSpec.AT_MOST
        heightMeasureSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

Integer.MAX_VALUE>>2
因为MeasureSpec 一个值表示了两个值,最高两位代表模式,后面代表的是尺寸
而这边makeMeasureSpec传的第一个参数是给子级的最大尺寸,所以右移两位就是能给子级最大的高度,所以解决了显示不全的问题

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值