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. python标准库介绍——1 os详解

    == os 模块 == ``os`` 模块为许多操作系统函数提供了统一的接口. 这个模块中的大部分函数通过对应平台相关模块实现, 比如 ``posix`` 和 ``nt. os`` 模块会在第一次导入 ...

  2. 强制删除一个Windows服务

    一个挂起的服务如下图所示,该服务相关的所有按钮都被禁用,包括启动.停止.暂停和恢复. 要停止这个服务,首先记住这个服务的名称,在这里是 ‘EntropySoftCFS’. 然后打开命令行窗口,运行 s ...

  3. Android开发13——内容提供者ContentProvider的基本使用

    一.ContentProvider简介 当应用继承ContentProvider类,并重写该类用于提供数据和存储数据的方法,就可以向其他应用共享其数据.ContentProvider为存储和获取数据提 ...

  4. nyoj322 sort 归并排序,树状数组

    Sort 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 You want to processe a sequence of n distinct integers b ...

  5. How to merge Scala Lists

    Scala List FAQ: How do I merge a List in Scala? NOTE: I wrote the solutions shown below a long time ...

  6. Python2 获取两日期之间的每一天

    import datetime def getEveryDay(begin_date,end_date): date_list = [] begin_date = datetime.datetime. ...

  7. javascript 中 IE与FF的不同之处及其兼容写法

    png透明 AlphaImageLoaderfilter:progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled=bEnabled,siz ...

  8. jquery和css3打造超梦幻的三维动画背景

    今天为大家带来的是一款由jquery和css3实现的超级梦幻的背景效果.绿色的小原点由远到近,由近到远一种飞跃效果.效果非常好看,我们一起看下效果图: 在线预览   源码下载 我们一起看下实现的代码. ...

  9. wp中的双向绑定

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Linq; usin ...

  10. 通过SiteMapDataSource动态获取SiteMap文件进行权限设置

    最近做一个用ASP.NET做一个小项目,用户不是很多,功能不算太复杂,但是做到权限控制的时候有点犯难,这么一个小系统如果全部做一个大的复杂的权限控制觉得成本不划算,打算用Treeview ,根据不同的 ...