博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ScrollView和ListView一起用时会冲突,解决办法
阅读量:6875 次
发布时间:2019-06-26

本文共 1269 字,大约阅读时间需要 4 分钟。

在很多情况下,ScrollView中要嵌套一个listView,但是由于两者都是滑动的,所以貌似在一起用不是很方便,这需要ListView重新测量自己的高度,具体方法如下:

第一步:要自定义一个listView,重新写里边的onMeasure()方法;

@Override  

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

第二步:写个计算listView每个Item的方法;

public void setListViewHeightBasedOnChildren(ListView listView) {

 

  // 获取ListView对应的Adapter

  ListAdapter listAdapter = listView.getAdapter();

  if (listAdapter == null) {

   return;

  }

  int totalHeight = 0;

  for (int i = 0; i < listAdapter.getCount(); i++) { // listAdapter.getCount()返回数据项的数目

   View listItem = listAdapter.getView(i, null, listView);

   listItem.measure(0, 0); // 计算子项View 的宽高

   totalHeight += listItem.getMeasuredHeight(); // 统计所有子项的总高度

  }

  ViewGroup.LayoutParams params = listView.getLayoutParams();

  params.height = totalHeight

    + (listView.getDividerHeight() * (listAdapter.getCount() - 1));

  // listView.getDividerHeight()获取子项间分隔符占用的高度

  // params.height最后得到整个ListView完整显示需要的高度

  listView.setLayoutParams(params);

 }

第三步:listview添加适配器后别忘了设置高度;

listView.setAdapter(adapter);  

new ListViewUtil().setListViewHeightBasedOnChildren(listView);

 

 

摘自:脚本之家。

转载于:https://www.cnblogs.com/ylt-niuniu/p/3403834.html

你可能感兴趣的文章
spring 配置文件applicationContext.xml命名空间及标签解析
查看>>
我的友情链接
查看>>
回到顶部代码(兼容IE6)
查看>>
web.xml文件的作用
查看>>
iOS开发篇——OC延展类目协议介绍
查看>>
期盼已久,不负所望!XenServer 6.5版本正式发布
查看>>
全方位解读云计算
查看>>
出来了,下雪了
查看>>
我的友情链接
查看>>
AFN网络图片获取
查看>>
Springboot官方构建下的mvnw.cmd的作用
查看>>
http://jquerymobile.com/ 手机特效制作
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
Oracle EBS R12查看用户密码
查看>>
爬虫书籍
查看>>
ORACLE SQL Loader
查看>>
linux 双网卡绑定
查看>>
Split-Brain
查看>>
rap接口管理工具
查看>>