put ListView in a ScrollView(bug fixed)
Android: put ListView in a ScrollView
When I need to use ListView with other controls on the same screen, and it doesn't fit the screen vertically, I wish to get ScrollView over all controls, to scroll entire screen. But, that's the point when "shit happens".
Android core developers say you "must not put ListView inside
ScrollView", because of many reasons, one of them - ListView has it's
own scroll.
When you try just do it, ListView collapses, and you wish it not to be collapsed at all.
To accomplish that, I use a "hack", measuring and setting the height directly:
public class Utility {
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre-condition
return;
} int totalHeight = 0;
int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.AT_MOST);
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(desiredWidth, MeasureSpec.UNSPECIFIED);
totalHeight += listItem.getMeasuredHeight();
} ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
}
Call this function right after you change ListView items, like that:
Utility.setListViewHeightBasedOnChildren(myListView);
Updated 9 Feb. 2011: fixed one nasty bug :) It all works now!
P.S. Original code is written by DougW at stackoverflow.com, I've modified it a bit.
put ListView in a ScrollView(bug fixed)的更多相关文章
- 解决android的ListView嵌套在ScrollView中不能被滚动的问题
使用滚动条容易带来一个后果,就是高度和宽度不受控制了, 之前就遇到一个已经有ScrollView的页面需要加个列表listView,然后就发现listView只看到前两行数据,下面的看不到,拉滚动条也 ...
- ListView inside a ScrollView
ScrollView里面放ListView, ListView无法展开的解决方法 http://stackoverflow.com/questions/18367522/android-list-vi ...
- 解决 listView gridView 与ScrollView嵌套时的冲突
package com.xqx.fight; import android.app.Activity; import android.os.Bundle; import android.view.Me ...
- ScrollView中嵌套ScrollView或ListView而且内部ScrollView或ListView也可滑动
1.ScrollView中嵌套ScrollView而且内部ScrollView也可滑动 (1)ScrollView继承类 public class InnerScrollView extends Sc ...
- ReactNative 分享解决listView的一个郁闷BUG
用ListView的时候,会出现一个非常傻bi的情况,就是render的时候,listView不显示,需要碰/滑一下才会显示. 一开始我在怀疑自己是不是布局哪里有冲突,改到哭都没发现布局有什么问题,直 ...
- Android中ListView嵌套进ScrollView时高度很小的解决方案
package com.example.test.util; import android.view.View; import android.view.ViewGroup; import andro ...
- ListView中添加ScrollView只显示一两行的问题
将ListView改为继承NoScrollListView package com.example.brtz.widget; import android.content.Context; impor ...
- ios下,微信小程序scrollview组件中的fixed元素抖得和帕金森病人一样
问题现象 这个问题是最近在优化小程序代码时发现的. 在ios环境下,微信小程序的scrollview组件包裹着一个position:fixed的view. 当在scrollview组件上滑动时,这个v ...
- 关于ScrollView嵌套ListView问题
Android开发之ScrollView中嵌套ListView的解决方案 原文:http://blog.csdn.net/minimicall/article/details/40983331 ...
随机推荐
- 【转】input file accept属性可以限制的文件类型
来源:http://blog.sina.com.cn/s/blog_6c9d65a10101a8yh.html 在上传文件的时候,需要限制指定的文件类型. <input type="f ...
- [hihoCoder] #1044 : 状态压缩·一
时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho在兑换到了喜欢的奖品之后,便继续起了他们的美国之行,思来想去,他们决定乘坐火车前往下一座城市——那座城市即将 ...
- C++操作 SQL数据库 实例 代码步骤
C++连接SQL数据库第一步 系统配置 1.设置SQLSERVER服务器为SQL登录方式,并且系统安全性中的sa用户要设置登录功能为“启用”,还有必须要有密码. 2.需要在ODBC中进行数据源配置,数 ...
- jquery中选择块并改变属性值的方法
本文为大家介绍下使用jquery改变class属性的值,通过removeClass.addClass实现,具体如下,感兴趣的朋友可以学习下jquery改变class属性的值 $("#top_ ...
- [LeetCode][Java] Search Insert Position
题目: Given a sorted array and a target value, return the index if the target is found. If not, return ...
- Spring Boot干货系列:(五)开发Web应用JSP篇
Spring Boot干货系列:(五)开发Web应用JSP篇 原创 2017-04-05 嘟嘟MD 嘟爷java超神学堂 前言 上一篇介绍了Spring Boot中使用Thymeleaf模板引擎,今天 ...
- 向linux服务器上传下载文件方式收集
向linux服务器上传下载文件方式收集 1. scp [优点]简单方便,安全可靠:支持限速参数[缺点]不支持排除目录[用法] scp就是secure copy,是用来进行远程文件拷贝的.数据传输使用 ...
- Android开发日记(三)
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentV ...
- 关联容器——map、set
map类型通常被称为关联数组,与正常数组类似,不同之处在于其下标不必是整数.我们通过一个关键字而不是位置来查找值(键值对). 与之相对,set就是关键字的简单集合.当只是想知道一个值是否存在时,set ...
- 人工智能时代,应立即学习python
人工智能时代,应立即学习python 应用:web开发,自动化运维开发,自动化测试,数据分析,机器学习 1.python 快速易学习2.python 基于web开发(zhihu:tornad web框 ...