安卓开发ScrollView嵌套ListView只显示一行
在用列表控件做一个“更多功能”的界面的时候
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ScrollView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <ListView
android:id="@+id/list_more_top"
android:layout_width="match_parent"
android:layout_height="fill_parent" >
</ListView>
<ImageView
android:id="@+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/divider" />
<ListView
android:id="@+id/list_more_center"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
<ImageView
android:id="@+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:src="@drawable/divider" />
<ListView
android:id="@+id/list_more_buttom"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</ScrollView>
原本用ScrollView嵌套ListView 但是在测试的时候 ListView列表只显示一列! 这不是我希望得到的
我希望的是可以整个拖动三个列表
于是上网查询 发现的问题所在
在ScrollView中嵌套ListView空间,无法正确的计算ListView的大小,导致只显示列表第一项
故可以通过代码,根据当前的ListView的列表项计算列表的尺寸。
package songsong; import com.example.xqx_tea.R; import android.app.Activity;
import android.app.AlertDialog;
import android.app.Notification.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.app.AlertDialog;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView; public class MenuMore extends Activity{
ListView list_more_top;
ListView list_more_center;
ListView list_more_buttom; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.menu_more); ListView list_more_top = (ListView) findViewById(R.id.list_more_top);
ListView list_more_center = (ListView) findViewById(R.id.list_more_center);
ListView list_more_buttom = (ListView) findViewById(R.id.list_more_buttom);
String[] adapterData_top = new String[] { "饮茶时刻表", "小茶叶大妙用","中国十大名茶"};
list_more_top.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData_top)); String[] adapterData_center = new String[] { "使用帮助", "意见反馈","软件介绍"};
list_more_center.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData_center)); String[] adapterData_buttom = new String[] { "检查更新", "退出软件"};
list_more_buttom.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,adapterData_buttom)); setListViewHeightBasedOnChildren(list_more_top);
setListViewHeightBasedOnChildren(list_more_center);
setListViewHeightBasedOnChildren(list_more_buttom); //为列表视图中选中的项添加响应事件
list_more_top.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
// TODO Auto-generated method stub }
}); //为列表视图中选中的项添加响应事件
list_more_center.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
// TODO Auto-generated method stub }
}); //为列表视图中选中的项添加响应事件
list_more_buttom.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) { // TODO Auto-generated method stub
if( == id)
{
finish();
}
if( == id)
{
Intent intent = new Intent();
intent.setClass(MenuMore.this, More_submitdeal.class);
startActivity(intent);
} }
});
} public void setListViewHeightBasedOnChildren(ListView listView) {
// 获取ListView对应的Adapter
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
return;
} int totalHeight = ;
for (int i = , len = listAdapter.getCount(); i < len; i++) {
// listAdapter.getCount()返回数据项的数目
View listItem = listAdapter.getView(i, null, listView);
// 计算子项View 的宽高
listItem.measure(, );
// 统计所有子项的总高度
totalHeight += listItem.getMeasuredHeight();
} ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight+ (listView.getDividerHeight() * (listAdapter.getCount() - ));
// listView.getDividerHeight()获取子项间分隔符占用的高度
// params.height最后得到整个ListView完整显示需要的高度
listView.setLayoutParams(params);
} }
给三个列表分别求出ListView完全显示需要的高度 便可以整体上下滑动三个列表了

安卓开发ScrollView嵌套ListView只显示一行的更多相关文章
- ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法(转)
ScrollView嵌套ListView只显示一行之计算的高度不正确的解决办法 分类: android应用开发2013-12-19 09:40 1045人阅读 评论(3) 收藏 举报 AndroidS ...
- ScrollView嵌套ListView只显示一行
错误描述 ScrollView嵌套ListView中导致ListView高度计算不正确,只显示一行. 解决方法 重写ListView的onMeasure方法,代码如下. @Override publi ...
- ScrollView嵌套ListView只显示一行解决方案
在ScrollView里边嵌套了个ListView,后边就发现数据源里好多数据,但ListView只是显示1行. 各种debug,打log,数据什么的都没问题,上网百度了下,发现原来是ScrollVi ...
- 冲突--ScrollView嵌套ListView只显示一行
在开发的过程当中,由于手机屏幕的大小的限制,我们经常需要使用滑动的方式,来显示更多的内容.在最近的工作中,遇见一个需求,需要将ListView嵌套到ScrollView中显示.于是乎有了如下布局: & ...
- 日积月累:ScrollView嵌套ListView只显示一行
在开发的过程当中,由于手机屏幕的大小的限制,我们经常需要使用滑动的方式,来显示更多的内容.在最近的工作中,遇见一个需求,需要将ListView嵌套到ScrollView中显示.于是乎有了如下布局: & ...
- 解决lScrollView嵌套ListView只显示一行的问题,listvie显示全部的item
ScrollView嵌套ListView只显示一行的问题 1.思路:给listview重新添加一个高度. listview的高度==listview.item的高度之和. 2.注意:关键是添加list ...
- 一键解决ScrollView嵌套ListView仅仅显示一行的问题
/** * 解决ScrollView嵌套ListView仅仅显示一行的问题 * * @param listView */ private void setListViewHeightBasedOnCh ...
- ScrollView中嵌套GridView,ListView只显示一行的解决办法
转载:http://blog.csdn.net/luohai859/article/details/39347583 关于为什么只显示一行,个人理解是:如果单独使用GridView和ListView, ...
- RecyclerView, ListView 只显示一行内容 问题解决
Adapter 中的data有多行,但是RecyclerView只显示一行. 原因出在item的layout xml, 用了自动生成的RelativeLayout, 她的默认高度height属性是ma ...
随机推荐
- 锚接口(下)——html5的history api
概述 虽然html5的history api是H5专门用来解决记录历史记录和单页面的方法,但是很多老式的浏览器并不支持它,所以一般遇到老式的浏览器会做一个polyfill使用之前的hashchange ...
- Swift5 语言指南(十三) 方法
方法是与特定类型相关联的函数.类,结构和枚举都可以定义实例方法,这些方法封装了用于处理给定类型的实例的特定任务和功能.类,结构和枚举也可以定义类型方法,它们与类型本身相关联.类型方法类似于Object ...
- Ubuntu16.04 + gtx1060 + cuda8.0 + cudnn5.1 + caffe + Theano + Tensorflow
参考 ubuntu16.04+gtx1060+cuda8.0+caffe安装.测试经历 ,细节处有差异. 首先说明,这是在台式机上的安装测试经历,首先安装的win10,然后安装ubuntu16.04双 ...
- LeetCode--No.001 Two Sum
Two Sum Total Accepted: 262258 Total Submissions: 1048169 Difficulty: Easy Given an array of integer ...
- DDD实战进阶第一波(四):开发一般业务的大健康行业直销系统(搭建支持DDD的轻量级框架三)
上一篇文章我们讲了经典DDD架构对比传统三层架构的优势,以及经典DDD架构每一层的职责后,本篇文章将介绍基础结构层中支持DDD的轻量级框架的主要代码. 这里需要说明的是,DDD轻量级框架能够体现DDD ...
- Linux_CentOS-服务器搭建 <四>
既然tomcat,弄好了,数据库安装好了.我们考虑考虑下.今天带给大家是, 数据库的还原备份: 备份开始: 登录开始: mysql -u root -p 创建一个测试用的数据库test并创 ...
- python-拷贝
1.普通的赋值操作 def print_id(array): ids = [] for ar in array: ids.append(id(ar)) print (array, ids) a = [ ...
- AWS DevOps – 配合Jenkins和CodeDeploy实现代码自动化部署
AWS DevOps – 配合Jenkins和CodeDeploy实现代码自动化部署 Amazon ElastiCache 连接至 Redis 节点 通过 AWS Command Line Inter ...
- Jfinal文件上传基础路径问题,windows下会以项目根路径为基础路径
在本地windows下开发测试文件上传 使用com.jfinal.cos进行multipart/form-data请求数据格式的文件上传解析 import com.jfinal.upload.Uplo ...
- 最小化安装centos5.5
安装LINUX的办法: 使用光盘 通过网络批量安装LINUX系统 先搭建一个LINUX做为安装的数据源(DHCP服务器,kickat) 设置所有其他要安装LINUX服务器的电脑以NET的方式启动,然后 ...