ScrollView嵌套ListView、GridView冲突

本文介绍了一种解决ScrollView中嵌套ListView导致显示不全的问题的方法。通过自定义ListView并重写onMeasure方法,使用MeasureSpec设置最大高度测量值,确保ListView能够完全展示其所有项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

之前 遇到ScrollView中嵌入ListView,GridView冲突的解决(让ListView全显示出来) 链接 
网上查找资料,代码大致如下:

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ListView;

public class ListViewMeasur extends ListView {

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

    public ListViewMeasur(Context context) {
        super(context);
    }

    public ListViewMeasur(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

其中对这段代码有疑惑:

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
//
        int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);
    }
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

网上也有解释的文章 比如链接 文章对 测量模式模式的讲解十分详细,但是本文通过源码来进一步理解。

int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,MeasureSpec.AT_MOST); 
 
  • 1
  • 1

作为参数onMeasure()方法,很明显作为代表测量的高度

super.onMeasure(widthMeasureSpec, expandSpec);
 
  • 1
  • 1

那接下来通过源码看一下调用的suMeasureSpec.makeMeasureSpec()方法。

    public static int makeMeasureSpec(int size, int mode) {
            if (sUseBrokenMakeMeasureSpec) {
                return size + mode;
            } else {
                return (size & ~MODE_MASK) | (mode & MODE_MASK);
            }
        }
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

MeasureSpec是一个32位的int值,其中高2位代表测量模式,低30位代表测量大小。 
在makeMeasureSpec中看大 return size + mode; 现在通过一段代码模拟一下这个过程。 
模拟makeMeasureSpec 输出的是10011111111111111111111111111111 
最高两位是10的时候表示”最大模式”。即MeasureSpec.AT_MOST

public class URShift {
    private static final int MODE_SHIFT = 30;
    public static final int AT_MOST     = 2 << MODE_SHIFT;
    public static void main(String[] args) {
        int i =Integer.MAX_VALUE ;
        System.out.println(Integer.toBinaryString(i));
        //~1111111111111111111111111111111

        System.out.println(Integer.toBinaryString(makeMeasureSpec(Integer.MAX_VALUE >> 2,
                AT_MOST)));
        //~10011111111111111111111111111111
    }
      public static int makeMeasureSpec(int size, int mode) {
          return size + mode;
      }
}
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

其实MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST)就是生成一个符合MeasureSpec的一个32位的包含测量模式和测量高度的int值。


转载地址:http://blog.youkuaiyun.com/wangbf_java/article/details/60151965

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值