在ScrollView中的listView gridView添加适配器之后添加
//设置gridView整体的高度
public void setListViewHeightBasedOnChildren(GridView gridView) {
// 获取ListView对应的Adapter

if(adapter== null) {
return;
}
int totalHeight = 0;
for(int i = 0, len = adapter.getCount(); i < len; i++) { //

listAdapter.getCount()返回数据项的数目
View listItem = adapter.getView(i, null, gridView);
listItem.measure(0, 0); // 计算子项View 的宽高
totalHeight += listItem.getMeasuredHeight(); // 统计所有子项

的总高度
}
ViewGroup.LayoutParams params = gridView.getLayoutParams();
params.height = totalHeight
+ (gridView.getHeight() * (adapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
gridView.setLayoutParams(params);
}

相当于把他们展开用ScrollView滚动查看
在listView gridView的父元素中添加 android:focusableInTouchMode="true"
android:focusable="true"可以避免他们展开时获取焦点让父元素获取焦点

在ScrollView中的 避免父控件拦截触摸事件
gridView.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
gridView.getParent().requestDisallowInterceptTouchEvent(true);
return false;//没消费当前事件
}
});

另一种方式 定义高度为wrap content  重写一个listView 重新算高度

<com.xqx.ximafm.app.widgets.FullListView

android:id="@+id/fullListView"

android:layout_width="match_parent"

android:layout_height="wrap_content">

public class FullListView extends ListView {

public FullListView(Context context) {

super(context);

}

public FullListView(Context context, AttributeSet attrs) {

super(context, attrs);

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

int expandSpec = MeasureSpec.makeMeasureSpec(

Integer.MAX_VALUE >> 2,     // size 控件可以显示的最大高度

MeasureSpec.AT_MOST);      //

super.onMeasure(widthMeasureSpec, expandSpec);

}

}

LinearLayout而言比较简单,由于 android:layout_width="match_parent",因此其width对应地widthSpec

mode值为MeasureSpec.EXACTLY

由于android:layout_width="wrap_content" , 因此其height对应地widthSpec mode值为

MeasureSpec.AT_MOST

推荐第二种方法   第一种方法的话如果布局不是linearlayout的话就会发生异常

最新文章

  1. Atitit.去除水印的方案
  2. JAVA教师:给JAVA初学者的忠告
  3. centos 开机启动服务
  4. javaWeb中 servlet 、request 、response
  5. CSS_css sprite原理优缺点及使用
  6. iOS-图片拉伸技巧
  7. Android Studio 设置不自动缩进匿名内部类
  8. 越狱Season 1-Episode 19: The Key
  9. web.xml中load-on-startup标签的含义
  10. 【英语】Bingo口语笔记(26) - Take系列
  11. Object类型
  12. C语言对象化编程
  13. Linkin大话Java和internet概念
  14. SQL查询中用到的函数
  15. jmeter分布式压测(多台电脑一起压测)
  16. 二、主流区块链技术特点及Hyperledger Fabric V0.6版本特点
  17. python 跨平台获取网卡信息和本机ip地址
  18. Flex 数组问题!
  19. vue 使用Slot 分发内容 学习总结。
  20. iptables 操作

热门文章

  1. pandas的to_csv函数
  2. Nexus3 仓库搭建(基于Docker)
  3. iOS TTF文件改变字体
  4. 长沙雅礼中学集训-------------------day2
  5. sqoop操作之ETL小案例
  6. SpringBoot入门篇--Thymeleaf引擎模板的基本使用方法
  7. Java 知识点(转)
  8. JQ-用户注册用到的图形验证码,短信验证码点击事件,切换active类
  9. LeaderF常用用法
  10. windows10系统右键添加cmd命令