Android开发ScrollView嵌套ListView只显示一行出现的原因以及解决办法

首先看ScrollView中的onMeasure()方法,之所以首先找onMeasure()方法是因为肯定是测量出现了问题,在onMeasure()方法种首先调用了super.onMeasure()方法,点进去之后可以看到在夫布局的onMeasure方法种调用了

measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);

measureChildWithMargins方法是ViewGroup中的一个方法,因为所有布局都是ViewGoup的子类,ScrollView也不例外,因此可以推测肯定是ScrollView重写了该方法导致问题的出现,在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,
                mPaddingLeft + mPaddingRight + lp.leftMargin + lp.rightMargin
                        + widthUsed, lp.width);
        final int usedTotal = mPaddingTop + mPaddingBottom + lp.topMargin + lp.bottomMargin +
                heightUsed;
        final int childHeightMeasureSpec = MeasureSpec.makeSafeMeasureSpec(
                Math.max(0, MeasureSpec.getSize(parentHeightMeasureSpec) - usedTotal),
                MeasureSpec.UNSPECIFIED);

        child.measure(childWidthMeasureSpec, childHeightMeasureSpec);
    }

很明显可以看到childHeightMeasureSpec的高度尺寸是Math.max(),高度模式是:UNSPECIFIED

那么再来看ListView中的onMeasure()方法,在ListView方法中有这么一个判断:

if (heightMode == MeasureSpec.UNSPECIFIED) {
        heightSize = mListPadding.top + mListPadding.bottom + childHeight +
                    getVerticalFadingEdgeLength() * 2;
    }

到这里已经可以找到问题的关键了,ScrollView对子控件的高度模式约束为尽可能最大,导致上面这个if条件成立,那么heightSize的高度也就只有padding值加上一个childHeight的高度了,那么怎样解决这个问题呢?最直接的办法就是想办法不让上面的判断成立,让下面这个判断成立:


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

那么就可以重写ListView的onMeasure()方法

public class MyListView extends ListView {
    public MyListView(Context context) {
        super(context);
    }

    public MyListView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        heightMeasureSpec=MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

 

 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值