zoukankan      html  css  js  c++  java
  • 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.

  • 相关阅读:
    classloader加载类原理
    要学的东西
    ArcGIS 10 帮助
    ArcGIS Server发布移动地图服务(mobile data access)时的问题
    SDE的性能取决于
    用oracle trace 来跟踪session
    ArcSDE10升级sp1后服务无法启动的解决办法
    ArcSDE 发布 WMS 服务出错
    ArcSDE10升级sp1后服务无法启动的解决办法
    ArcGIS Server发布移动地图服务(mobile data access)时的问题
  • 原文地址:https://www.cnblogs.com/kobe8/p/3153961.html
Copyright © 2011-2022 走看看