安卓开发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 ...
随机推荐
- kubernetes集群搭建(8):常用命令
kubectl create -f xxxx.yaml #创建rc.deploy.svc等 kubectl delete -f xxxx.yaml #删除对应的创建 kubectl get pods ...
- Spring Boot整合 Thymeleaf 模板引擎
什么是Thymeleaf Thymeleaf是一款用于渲染XML.XHTML.HTML5内容的模板引擎.类似Velocity,FreeMaker模板引擎,它也可以轻易的与Spring MVC等Web框 ...
- rsync排除多个文件实现同步
首先创建exclude目录放入xx.list排除文件. [root@localhost tmp]# cat /exclude/a_exclude.list a.txt lai ———————————— ...
- .net core内部分享ppt
Microsoft .NET 自 2002 年发行 v1.0 以来,已经过了近 14 个年头,在这 14 年里面,.NET 日渐成熟并成为 Microsoft 的重要开发平台之一,只要是在 Windo ...
- windows下更新node环境
https://github.com/Kenshin/gnvm 下载gnvm.exe程序 使用where node命令查看node所在目录,并将下载好的gnvm.exe程序复制到目录中 输入gnvm ...
- log4j学习总结
一直使用log4j来记录日志,但是一直以来没有深入研究过log4j,最近研究了下log4j,下面总结一下: log4j配置: 1. 配置根Logger,其语法为: log4j.rootLogger = ...
- kdump内核转储
目录 CentOS 7.5 配置Kdump 安装Kdump 安装Kdump图形化 配置保留内存 配置kdump类型 核心转储到本地 核心转储到设备 使用NFS指定核心转储 使用SSH指定核心转储 配置 ...
- Java并发编程笔记之Timer源码分析
timer在JDK里面,是很早的一个API了.具有延时的,并具有周期性的任务,在newScheduledThreadPool出来之前我们一般会用Timer和TimerTask来做,但是Timer存在一 ...
- leetcode — generate-parentheses
import java.util.ArrayList; import java.util.Arrays; import java.util.List; /** * Source : https://o ...
- SpringMVC教程1
一.SpringMVC介绍 1.MVC介绍 ==模型-视图-控制器(MVC== 是一个众所周知的以设计界面应用程序为基础的设计模式.它主要通过分离模型.视图及控制器在应用程序中的角色将业务逻辑从界面中 ...