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. springmvc+spring+mybatis+maven项目构建

    1.首先在myeclipse10中安装maven的插件,将插件放入D:\Program Files (x86)\myEclipse10\MyEclipse Blue Edition 10\dropin ...

  2. SyntaxError: missing ] after element list 火狐问题

    关于火狐运行var obj = eval('(' + data + ')');时 报SyntaxError: missing ] after element list错误,Chrome和IE正常 情形 ...

  3. TransitiveClosure

    http://acm.nudt.edu.cn/~twcourse/TransitiveClosure.html https://en.wikipedia.org/wiki/Closure_(mathe ...

  4. python 分别用python2和python3伪装浏览器爬取网页内容

    python网页抓取功能非常强大,使用urllib或者urllib2可以很轻松的抓取网页内容.但是很多时候我们要注意,可能很多网站都设置了防采集功能,不是那么轻松就能抓取到想要的内容. 今天我来分享下 ...

  5. 临界区&Monitor

    监视器(Monitor)的概念 可以在MSDN(http://msdn.microsoft.com/zh-cn/library/ms173179(VS.80).aspx)上找到下面一段话: 与lock ...

  6. LeetCode: Permutations 解题报告

    Permutations Given a collection of numbers, return all possible permutations. For example,[1,2,3] ha ...

  7. LeetCode: Word Search 解题报告

    Word SearchGiven a 2D board and a word, find if the word exists in the grid. The word can be constru ...

  8. java 多线程6: 中断机制 优雅的终止java线程

    前文 java 多线程5: java 终止线程及中断机制 (stop().interrupt() .interrupted().isInterrupted()) 使用 interrupt() 和 in ...

  9. Python中写一个乒乓球类的游戏

    最近开始学Python,感觉挺好玩的,既有脚本语言的灵活性,又有丰富的类库与面向对象的特点,开发起来很方便. 游戏的规则和乒乓球一样,如果妙蛙种子掉地上了就算输,你可以用蓝色的跷跷板弹它,使他不落到地 ...

  10. 友盟分享和cocos2dx符合重复duplicate symbol 解决方案

    最近使用友盟分享的sdk,没想到libWechatSDK.a居然和cocos2dx的符合冲突,提示base64.o重复了. 于是到网上找了一下解决方案,基本上去除微信的base64.o即可了. 用ar ...