解决ScrollView中嵌套ListView滚动效果冲突(ListView只显示一行)
2015-06-08 16:12
204 查看
在开发的过程当中,由于手机屏幕的大小的限制,我们经常需要使用滑动的方式,来显示更多的内容。在最近的工作中,遇见一个需求,需要将ListView嵌套到ScrollView中显示。于是乎有了如下布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFE1FF"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="vertical"
android:fadingEdgeLength="5dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
运行程序,如下结果,无论你如何调整layout_width,layout_height属性,ListView列表只显示一列!
在查阅的各种文档和资料后,发现在ScrollView中嵌套ListView空间,无法正确的计算ListView的大小,故可以通过代码,根据当前的ListView的列表项计算列表的尺寸。实现代码如下:
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
String[] adapterData = new String[] { "Afghanistan", "Albania",… … "Bosnia"};
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData));
setListViewHeightBasedOnChildren(listView);
}
public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
// 计算子项View 的宽高
listItem.measure(0, 0);
// 统计所有子项的总高度
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
}
运行结果,OK问题搞定,打完收工!
我每次调用的时候都会产生在listItem.measure(0,0)报空指针异常。我debug 发现listItem 并不是为空啊,为啥会报错。在经过一番查找之后。我发现原来是自己item的布局用了RelativeLayout 把他换为LinearLayout 就好了。究其原因,原来是 Linearlayout重写了onmeasure方法,其他的布局文件没有重写onmeasure,所以在调用listItem.measure(0,
0); 会报空指针异常,如果想用这个东东,就必须用linearlayout布局喽。
本文出自 无忧之路 - 博客园
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFE1FF"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadingEdge="vertical"
android:fadingEdgeLength="5dp" />
</LinearLayout>
</ScrollView>
</LinearLayout>
运行程序,如下结果,无论你如何调整layout_width,layout_height属性,ListView列表只显示一列!
在查阅的各种文档和资料后,发现在ScrollView中嵌套ListView空间,无法正确的计算ListView的大小,故可以通过代码,根据当前的ListView的列表项计算列表的尺寸。实现代码如下:
public class MainActivity extends Activity {
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView1);
String[] adapterData = new String[] { "Afghanistan", "Albania",… … "Bosnia"};
listView.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData));
setListViewHeightBasedOnChildren(listView);
}
public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
}
int totalHeight = 0;
for (int i = 0, len = listAdapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
// 计算子项View 的宽高
listItem.measure(0, 0);
// 统计所有子项的总高度
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - 1));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
}
}
运行结果,OK问题搞定,打完收工!
解决ListView 跟ScroolView 共存 listItem.measure(0, 0) 空指针
在网上找到ListView 和ScroolView 共存的方法无非是给他每个listview 重新增加高度,但是android 的设计者始终认为这并不是一种好的实现方法。但是有的时候有必须要用这种蛋疼的设计。
public class Utility { public static void setListViewHeightBasedOnChildren(ListView listView) { ListAdapter listAdapter = listView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; for (int i = 0; i < listAdapter.getCount(); i++) { View listItem = listAdapter.getView(i, null, listView); listItem.measure(0, 0); totalHeight += listItem.getMeasuredHeight(); } ViewGroup.LayoutParams params = listView.getLayoutParams(); params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); listView.setLayoutParams(params); } }
我每次调用的时候都会产生在listItem.measure(0,0)报空指针异常。我debug 发现listItem 并不是为空啊,为啥会报错。在经过一番查找之后。我发现原来是自己item的布局用了RelativeLayout 把他换为LinearLayout 就好了。究其原因,原来是 Linearlayout重写了onmeasure方法,其他的布局文件没有重写onmeasure,所以在调用listItem.measure(0,
0); 会报空指针异常,如果想用这个东东,就必须用linearlayout布局喽。
本文出自 无忧之路 - 博客园
相关文章推荐
- 【搬运】Android四大基本组件介绍与生命周期
- Windows Sockets 2.0 新特性
- JMeter快速应用
- 博客博文
- Winform 自定义图片菜单控件
- Entity Framework查询生成大量的子查询,如何避免?求救
- SQL-select
- linux下通用Makefile写法
- 命令行tomcat添加到服务成功,服务中却死活找不到
- 做科研的几点体会:如何多发 SCI
- camera拍摄照片,保存显示
- Struts 2 标签库及使用
- 正则表达式
- 引用的难点:函数返回值是引用(引用当左值)
- c# winform 获取当前程序运行根目录
- 引用的难点:函数返回值是引用(引用当左值)
- 封装jQuery Validate扩展验证方法
- 利用 org.apache.commons.io.FileUtils快速读写文件
- (转)java垃圾回收二
- AutoCAD .NET二次开发(二)