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)的更多相关文章

  1. 解决android的ListView嵌套在ScrollView中不能被滚动的问题

    使用滚动条容易带来一个后果,就是高度和宽度不受控制了, 之前就遇到一个已经有ScrollView的页面需要加个列表listView,然后就发现listView只看到前两行数据,下面的看不到,拉滚动条也 ...

  2. ListView inside a ScrollView

    ScrollView里面放ListView, ListView无法展开的解决方法 http://stackoverflow.com/questions/18367522/android-list-vi ...

  3. 解决 listView gridView 与ScrollView嵌套时的冲突

    package com.xqx.fight; import android.app.Activity; import android.os.Bundle; import android.view.Me ...

  4. ScrollView中嵌套ScrollView或ListView而且内部ScrollView或ListView也可滑动

    1.ScrollView中嵌套ScrollView而且内部ScrollView也可滑动 (1)ScrollView继承类 public class InnerScrollView extends Sc ...

  5. ReactNative 分享解决listView的一个郁闷BUG

    用ListView的时候,会出现一个非常傻bi的情况,就是render的时候,listView不显示,需要碰/滑一下才会显示. 一开始我在怀疑自己是不是布局哪里有冲突,改到哭都没发现布局有什么问题,直 ...

  6. Android中ListView嵌套进ScrollView时高度很小的解决方案

    package com.example.test.util; import android.view.View; import android.view.ViewGroup; import andro ...

  7. ListView中添加ScrollView只显示一两行的问题

    将ListView改为继承NoScrollListView package com.example.brtz.widget; import android.content.Context; impor ...

  8. ios下,微信小程序scrollview组件中的fixed元素抖得和帕金森病人一样

    问题现象 这个问题是最近在优化小程序代码时发现的. 在ios环境下,微信小程序的scrollview组件包裹着一个position:fixed的view. 当在scrollview组件上滑动时,这个v ...

  9. 关于ScrollView嵌套ListView问题

    Android开发之ScrollView中嵌套ListView的解决方案   原文:http://blog.csdn.net/minimicall/article/details/40983331   ...

随机推荐

  1. Android开发7——android.database.CursorIndexOutOfBoundsException:Index -1 requested

    android中数据库处理使用cursor时,游标不是放在为0的下标,而是放在为-1的下标处开始的. 也就是说返回给cursor查询结果时,不能够马上从cursor中提取值. 下面的代码会返回错误Us ...

  2. SpringBoot进阶

    一.表单验证 二.AOP处理请求 AOP是一种编程范式.与语言无关,是一种程序设计思想. 面向过程到面向对象.换个角度看世界,换个姿势处理问题. 2.1AOP实例-http请求 MAVEN添加依赖:o ...

  3. 在 ASP.NET Web API 中,使用 命名空间(namespace) 来作为路由的参数

    这个问题来源于我想在 Web API 中使用相同的控制器名称(Controller)在不同的命名空间下,但是 Web API 的默认 路由(Route) 机制是会忽略命名空间的不同的,如果这样做,会看 ...

  4. java线程高并发编程

    java线程具体解释及高并发编程庖丁解牛 线程概述: 祖宗: 说起java高并发编程,就不得不提起一位老先生Doug Lea,这位老先生可不得了.看看百度百科对他的评价,一点也不为过: 假设IT的历史 ...

  5. 批处理学习笔记1 - Hellow World

    记录自己学习批处理的一点总结吧. 批处理的好处: 可以配合vs,在build完文件之后执行自己的批处理命令. 可以批量修改文件名,或者进行复杂的查询等,对文件可编程操作. 从Hellow world开 ...

  6. httpwebrequest异步参考

    http://www.cnblogs.com/SanMaoSpace/archive/2011/07/27/2118133.html http://www.cnblogs.com/qianlifeng ...

  7. 【Android】6.4 DatePickerDialog和TimePickerDialog

    分类:C#.Android.VS2015: 创建日期:2016-02-08 一.简介 在Android应用中,日期选择对话框和时间选择对话框是分别提供的. 日期选择对话框(DatePickerDial ...

  8. Tomcat Jboss Glassfish 三种常见web容器比较

    一.缘由: 新公司平台是纯Java架构,有用到Java Web(JSP).Java 业务(EJB).Nginx..Websphere MQ.Mysql这样.大家知道java是跑在容器里的, 这里的业务 ...

  9. Effective JavaScript Item 46 优先使用数组而不是Object类型来表示有顺序的集合

    本系列作为Effective JavaScript的读书笔记. ECMAScript标准并没有规定对JavaScript的Object类型中的属性的存储顺序. 可是在使用for..in循环对Objec ...

  10. 壮哉大微软,.Net人的春天来了,你准备好了嘛!

    http://www.cnblogs.com/hguchen/p/5621809.html *********************** 一.序言 2016年6月27日的早晨,微软在 Red Hat ...