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 ...
随机推荐
- PHP利用MySQL保存session(php5.4之前的处理)
简介 使用MySQL保存session,需要保存三个关键性的数据:session id.session数据.session生命期. 考虑到session的使用方式,没必要使用InnoDB引擎,MyIS ...
- linux怎么查看一个文件夹的大小
linux查看一个文件夹的大小的命令为: -lh 该文件夹的完整路径 例,查询/var文件夹的大小: -lh /var du 递归查询该路径下所有文件的大小(若不加任何参数,则显示文件夹内的所有文件, ...
- Spring里的FactoryBean和BeanFactory有啥区别?
分别看这俩文章就知道了 Spring的FactoryBean使用 Spring加载xml配置文件的方式 ApplicationContext
- [na][QoS]cisco3560限速配置案例-收集于网工泡泡
网络中常用到这些:CISCO和H3C-MAC过滤+端口限速+端口镜像+端口隔离 不同的方式不同的思想:嘎嘎 其他各个厂商的限速链接:http://pan.baidu.com/s/1hrIMoSG 密码 ...
- RAID卡 BBU Learn Cycle周期的影响
背景 最近遇到有些带MegaSAS RAID卡的服务器,在业务高峰时突然IO负载飚升得很高,IO性能急剧下降,查了日志及各种设置最后才发现是RAID卡的Cache写策略由 WriteBack变成Wri ...
- C++ HOJ 猴子分桃
[题目描写叙述] 老猴子辛苦了一辈子,给那群小猴子们留下了一笔巨大的財富--一大堆桃子.老猴子决定把这些桃子分给小猴子. 第一个猴子来了,它把桃子分成五堆,五堆一样多,但还多出一个.它把剩下的一个留给 ...
- java多线程实验 滚动字
package com.rgy.Test; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt ...
- Parallel Programming AND Asynchronous Programming
https://blogs.oracle.com/dave/ Java Memory Model...and the pragmatics of itAleksey Shipilevaleksey.s ...
- angular -- $routeParams API翻译
原api出处: https://docs.angularjs.org/api/ngRoute/service/$routeParams $routeParams 可以获取当前路径参数. 需要ngrou ...
- arm程序的反汇编程序
这是汇编源文件: MCU:S3C2440(arm920T) 代码实现点亮个led小灯 .text .global _start _start: ldr r0,=0x56000010 @GPBCON m ...