处理ScrollView嵌套GridView显示不全问题
2016-12-13 13:10
134 查看
public class FixedViewUtil { @TargetApi(Build.VERSION_CODES.JELLY_BEAN) public static void setListViewHeightBasedOnChildren(GridView listView, int col) { // 获取listview的adapter ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { return; } // 固定列宽,有多少列 int totalHeight = 0; // i每次加4,相当于listAdapter.getCount()小于等于4时 循环一次,计算一次item的高度, // listAdapter.getCount()小于等于8时计算两次高度相加 for (int i = 0; i < listAdapter.getCount(); i += col) { // 获取listview的每一个item View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); // 获取item的高度和 totalHeight += listItem.getMeasuredHeight(); totalHeight += listView.getVerticalSpacing(); if (i==listAdapter.getCount()-1) { totalHeight += listView.getVerticalSpacing(); } } // 获取listview的布局参数 LayoutParams params = listView.getLayoutParams(); // 设置高度 params.height = totalHeight; // 设置margin ((MarginLayoutParams) params).setMargins(10, 10, 10, 10); // 设置参数 listView.setLayoutParams(params); } public static void setListViewHeightBasedOnChildren(ListView lv){ ListAdapter listAdapter = lv.getAdapter(); int listViewHeight = 0; int adaptCount = listAdapter.getCount(); for(int i=0;i<adaptCount;i++){ View temp = listAdapter.getView(i,null,lv); temp.measure(0,0); listViewHeight += temp.getMeasuredHeight(); } LayoutParams layoutParams = lv.getLayoutParams(); layoutParams.width = LayoutParams.MATCH_PARENT; layoutParams.height = listViewHeight; lv.setLayoutParams(layoutParams); } }
在代码中使用即可
FixedViewUtil.setListViewHeightBasedOnChildren(mRecommendGv, 2);
相关文章推荐
- 在ListView嵌套GridView中出现的点击事件冲突的处理以及gridview显示不全的问题
- Android 在ListView嵌套GridView中出现的点击事件冲突的处理以及gridview显示不全的问题
- [Android]在ListView嵌套GridView中出现的点击事件处理以及gridview显示不全的问题
- scrollview嵌套listview 数据显示不全 问题处理方式集合
- [Android]在ListView嵌套GridView中出现的点击事件处理以及gridview显示不全的有关问题
- Exchange 2013 OU超过500 显示不全问题处理
- android RecycleView 嵌套问题切换页面跳动问题、嵌套展开显示不全问题处理方法
- 解决MSChart底部横坐标显示不全的问题
- VC读取mysql数据库中文字符的时候全部显示问号问题的处理
- J2ME 用来处理手机游戏中长字符串显示问题,自己写的,仅作参考
- 【转】 【字符集】处理Toad显示乱码及Windows XP下无法插入“某些汉字”问题
- 解决,windows的任务计划中添加一个批处理脚本,总是显示命令行窗口的问题
- Delphi下Label中文字体显示不全问题的解决
- tomcat中文显示问题处理
- “IE7中Frameset页面显示不全”问题的原因与解决方案 (转)
- “IE7中Frameset页面显示不全”问题的原因与解决方案
- AJAX tab显示不全及Android ADT不能下载的问题(已解决)
- “IE7中Frameset页面显示不全”问题的原因与解决方案
- JS窗口问题处理:使弹出窗口保持前端显示的几种方法,及window窗体对象open()和showModalDialog()用法
- 固定宽度下拉列表中option内容显示不全问题解决方法