关于禁止滑动的ListView

前段时间因项目需要ScrollView与ListView嵌套,为了解决滑动冲突,我想禁止ListView滑动,所以上网查了一下如何禁止ListView滑动,其中,我采用了如下方法:

public class NoScrollListView extends ListView{  
  
     public NoScrollListView(Context context, AttributeSet attrs){  
          super(context, attrs);  
     }  
  
     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec){  
          int mExpandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);  
          super.onMeasure(widthMeasureSpec, mExpandSpec);  
     }  
} 

继承ListView的类,重写onMeasure方法,这样ListView就不会滑动了。原理就是将ListView的测量高度设置成无穷大。下面来解释一下。


关于MeasureSpec.mackeMeasurSpec(int size, int mode)方法:

这个方法会将两个参数合成一个数字,这个数字意义如下图所示:(widthMeasureSpec与heightMeasureSpec就是这种数字)


首先我来解释后一个参数mode(模式)。mode有三个常量值:

1.精确模式 MeasureSpec.EXACTLY。合成数字最高两位是01的时候表示"'精确模式"。

2.最大模式 MeasureSpec.AT_MOST。合成数字最高两位是11的时候表示"最大模式"。

3.未指定模式 MeasureSpec.UNSPECIFIED。合成数字最高两位是00的时候表示"未指定模式"。

让我们来看一下官网的英文解释:(我知道大家都看得懂英文的,我就不翻译了大笑偷笑

Constants

  AT_MOST

Measure specification mode: The child can be as large as it wants up to the specified size.

  EXACTLY

Measure specification mode: The parent has determined an exact size for the child.

  UNSPECIFIED

Measure specification mode: The parent has not imposed any constraint on the child.

(下面这段是详细解释,可看可不看)

精确模式 MeasureSpec.EXACTLY:当控件的layout_width或layout_height为具体数字(如50dp)或者为fill_parent的时候,都是精确尺寸;

最大模式 MeasureSpec.AT_MOST:当控件的layout_width或layout_height指定为wrap_content时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。

未指定模式 MeasureSpec.UNSPECIFIED:这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。

官网英文解释:

1.UNSPECIFIED: This is used by a parent to determine the desired dimension of a child View. For example, a LinearLayout may call measure() on its child with the height set to UNSPECIFIED and a width of EXACTLY 240 to find out how tall the child View wants to be given a width of 240 pixels.

2.EXACTLY: This is used by the parent to impose an exact size on the child. The child must use this size, and guarantee that all of its descendants will fit within this size.

3.AT MOST: This is used by the parent to impose a maximum size on the child. The child must guarantee that it and all of its descendants will fit within this size.



好了,关于MeasureSpec.mackeMeasurSpec(int size, int mode)中的前一个变量size,顾名思义,就是view的size啦,也就是合成数字的后面部分。


然后,再来说说Integer.MAX_VALUE >> 2的意思。

Integer.MAX_VALUE是一个数字,>>2意思是将这个数字的二进制右移两位,左边空出的两位就用MeasureSpec.mackeMeasurSpec(int size, int mode)中的mode代表的数字拼接上。为了确保ListView内容无穷大,我们才用了一个无穷大的数:Integer.MAX_VALUE。但这样的ListView有一个缺点,由于ListView设为了无穷大,所以ListView自己的回收机制也没有了,加载了多少条信息就加载了多少view,所以Item过多的话会导致界面卡顿。


好了,这样解释应该都能懂吧?大笑

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值