android——ListView和ScrollView嵌套只显示一条问题

1、原因:查看源码发发现ListView的模式是UNSPECIFIED,是top+bottom+一条item的高

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

2、所以结局方法就是要自定义ListView,重新设置测量模式

public class CustomListView extends ListView {
    public CustomListView(Context context) {
        this(context,null);
    }

    public CustomListView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

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

    /**
     *  解决listview和scrollView嵌套只显示一条问题(重新设置模式)
     *  因为ListView的模式是UNSPECIFIED,是top+bottom+一条item的高
     *
     *      if (heightMode == MeasureSpec.UNSPECIFIED) {
     *             heightSize = mListPadding.top + mListPadding.bottom + childHeight +
     *                     getVerticalFadingEdgeLength() * 2;
     *         }
     *
     *  右移2位是AT_MOST共32位,前两位是模式信息,后30位是值,后移2位获得30位的值
     *
     *  Integer.MAX_VALUE:不设置成这个就会走下面方法只显示一条
     *
     *     if (returnedHeight >= maxHeight) {
     *                 // We went over, figure out which height to return.  If returnedHeight > maxHeight,
     *                 // then the i'th position did not fit completely.
     *                 return (disallowPartialChildPosition >= 0) // Disallowing is enabled (> -1)
     *                             && (i > disallowPartialChildPosition) // We've past the min pos
     *                             && (prevHeightWithoutPartialChild > 0) // We have a prev height
     *                             && (returnedHeight != maxHeight) // i'th child did not fit completely
     *                         ? prevHeightWithoutPartialChild
     *                         : maxHeight;
     *             }
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE>>2,MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_26554909

有您的支持,我们将做的更好

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值