安卓开发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 ...
随机推荐
- Android交流会-碎片Fragment,闲聊单位与尺寸
女孩:又周末了哦~ 男孩:那么今日来开个交流会,我们也学一学人家高大尚的大会,自己开一个,广州站,Android开发攻城狮交流会~ 1.Fragment概要: Android从3.0开始引入了Frag ...
- HTTP请求时间参数设置
1. JSON 2019-01-18 18:36:35 2. Postman 2019/01/18 18:36:35
- 【LeetCode】21.合并两个有序链表
题目 将两个有序链表合并为一个新的有序链表并返回.新链表是通过拼接给定的两个链表的所有节点组成的. 示例: 输入:1->2->4, 1->3->4 输出:1->1-> ...
- 项目初始化以后出现:Unable to load script from assets 'index.android.bundle
Mac中真机测试React Native project时出现Unable to load script from assets 'index.android.bundle' 2018年01月21日 ...
- ubuntu18.04 运行时提示缺少libstdc++.so.6
解决方法:输入命令 sudo apt- 提示:ubuntu默认软件包管理器不是yum,而是dpkg,安装软件时用apt-get PS:在ubuntu下最好不要去装yum,不然可能会出现一些奇怪的问题
- GraphX之Pregel(BSP模型-消息传递机制)学习
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreem ...
- [,,].length等于几
分别测试了谷歌.欧朋,火狐,QQ.搜狗,Edge,ie5.7.8.9.10.11 其中ie5,ie7,ie8得到的结果为3 其他均为2:如果最后一个逗号后面为空,则不识别最后一位
- Python:SQLMap源码精读—基于错误的盲注(error-based blind)
目标网址 http://127.0.0.1/shentou/sqli-labs-master/Less-5/?id=1 Payload的生成 <test> <title>MyS ...
- orcal10g下载地址
http://download.oracle.com/otn/nt/oracle10g/10201/102010_win64_x64_database.zip https://updates.orac ...
- leetcode — container-with-most-water
/** * Source : https://oj.leetcode.com/problems/container-with-most-water/ * * Created by lverpeng o ...