安卓开发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 ...
随机推荐
- 报警系统:php输出头信息以方便脚本抓取信息[排查篇]
做监控系统时,需要对某个页面进行监控,可以通过很多方式进行报警,如:正常则输出一个规定的变量,错误时则不输出.但是还有一个更为方便的做法,就是当前错误时,直接使用header抛出信息,如: heade ...
- ARM中几个典型的汇编指令解析
启动嵌入式设备时,遇到了一些汇编,做个笔记,免得以后忘记了. 一句汇编语句如下所指示: __asm ( ".syntax unified\n" ".thumb\n&quo ...
- Testing - 软件测试知识梳理 - 软件性能测试
软件性能测试的基本概念 软件的性能是软件的一种非功能特性,它关注的不是软件是否能够完成特定的功能,而是软件在完成该功能时展示出来的及时性. 软件性能的指标 响应时间:是指系统对请求作出响应的时间,并且 ...
- Python常用模块time & datetime &random 模块
时间模块前言 在Python中,与时间处理有关的模块就包括:time,datetime 一.在Python中,通常有这几种方式来表示时间: 时间戳 格式化的时间字符串 元组(struct_time)共 ...
- Jupyter中python3之numpy练习
---恢复内容开始--- Numpy_pratice In [2]: n = 10 L = [i for i in range(n)] In [3]: L * 2 Out[3]: [0, 1, 2, ...
- vue项目打包上线时的配置操作
vue的图片路径,和背景图片路径打包后错误解决 2017-12-11 16:00 by muamaker, 7037 阅读, 0 评论, 收藏, 编辑 最近在研究vue,老实的按照官网提供的,搭建的了 ...
- app操作的一些命令
这里的操作都是在windows下,在android SDK安装好之后就可以连接实体手机或者模拟器操作 1.查看连接的手机或者模拟器 adb devices 结果如下: 2.查看某个app的包名和act ...
- list源码3(参考STL源码--侯捷):push_front、push_back、erase、pop_front、pop_back、clear、remove、unique
list源码1(参考STL源码--侯捷):list节点.迭代器.数据结构 list源码2(参考STL源码--侯捷):constructor.push_back.insert list源码3(参考STL ...
- Xcode 8.X Command Line Tools
Summary Step 1. Upgrade Your System to macOS Sierra Step 2. Open the Terminal Application Step 3. Is ...
- Java SE 9(JDK9)环境安装及交互式编程环境Jshell使用示例
目的 安装JDK 9, 练习Jshell工具的使用, 体验Java的交互式编程环境. 什么是Jshell 其实就是一个命令行工具,安装完JDK9后,可以在bin目录下找到该工具,与Python的解释器 ...