ScrollView嵌套ListView,.measure(0,0);空指针NullException
2015-08-04 13:25
337 查看
First: SrollView嵌套ListView时显示不全问题及ListView高度问题: 直接上代码,使用以下函数解决此问题:
public void setListViewHeightOnChildren(ListView listView) { //adapter为ListView的适配器 if (adapter == null) { return; } int totalHeight = 0; for (int i = 0, len = adapter.getCount(); i < len; i++) { // 返回数据项的数目 View listItem = adapter.getView(i, null, listView); // 子项View 的宽高 listItem.measure(0, 0); // 计算所有子项的总高度 totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); // 获取子项间分隔符占用的高度,adapter.getCount() == 1 ? 2 : adapter.getCount() - 1这个判断是为了解决特殊情况(比如Item有设置MarginTop....),当只有一个Item时滑动出现显示不全及被截取一部分问题; params.height = totalHeight + (listView.getDividerHeight() * (adapter.getCount() == 1 ? 2 : adapter.getCount() - 1)); listView.setLayoutParams(params); }
Two: 当调用listItem.measure(0, 0);报空指针时问题: 检查Adapter适配时Item的根容器为RelativeLayout, 报错原因: In platform version 17 and lower, RelativeLayout was affected by a measurement bug that could cause child views to be measured with incorrect MeasureSpec values. (See MeasureSpec.makeMeasureSpec for more details.) This was triggered when a RelativeLayout container was placed in a scrolling container, such as a ScrollView or HorizontalScrollView. If a custom view not equipped to properly measure with the MeasureSpec mode UNSPECIFIED was placed in a RelativeLayout, this would silently work anyway as RelativeLayout would pass a very large AT_MOST MeasureSpec instead. This behavior has been preserved for apps that set android:targetSdkVersion="17" or older in their manifest's uses-sdktag for compatibility. Apps targeting SDK version 18 or newer will receive the correct behavior 有三种解决方案: 一、升级版本到4.2.2 二、更改根容器为LinearLayout 三、在适配器里添加convertView.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); convertView为Item的view
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories