zoukankan      html  css  js  c++  java
  • ListView在ScrollView中不显示全部的问题

      在实际应用中,我们可能会遇到把ListView放到ScrollView中的情况,在这种情况下,ListView的滑动属性与ScrollView的滑动出现冲突,从而ListView只显示一项。这里提供一种解决方案,就是设置ListView中每个Item的高度显示之,使他失去滑动的属性,从而适应ScrollView的滑动。只需在填充数据之后设置即可:

        /**
         * 通过设置它的高度,让它“放弃”自身滚动属性,而放置到ScrollView中使用的。
         * 
         * @param lv
         */
        private void setListViewHeight(ListView lv) {
            ListAdapter la = lv.getAdapter();
            if (null == la) {
                return;
            }
            // calculate height of all items.
            int h = 0;
            final int cnt = la.getCount();
            for (int i = 0; i < cnt; i++) {
                View item = la.getView(i, null, lv);
                item.measure(0, 0);
                h += item.getMeasuredHeight();
            }
            // reset ListView height
            ViewGroup.LayoutParams lp = lv.getLayoutParams();
            lp.height = h + (lv.getDividerHeight() * (cnt - 1));
            lv.setLayoutParams(lp);
        }
  • 相关阅读:
    hdu 1542 Atlantis
    cf C. Cupboard and Balloons
    cf C. Tourist Problem
    hdu 4027 Can you answer these queries?
    hdu 1255 覆盖的面积
    hdu 1698 Just a Hook
    zoj 1610 Count the Colors
    hdu 4302 Holedox Eating
    hdu 4288 Coder
    tsne理论学习
  • 原文地址:https://www.cnblogs.com/lossingdawn/p/4828203.html
Copyright © 2011-2022 走看看