zoukankan      html  css  js  c++  java
  • ScrollView中嵌套ListView

    scrollview中嵌入listview,要是直接把listview嵌进scrollview中,listview的高度是固定的不能进行滑动。默认情况下Android是禁止在ScrollView中放入另外的ScrollView的,它的高度是无法计算的。这就导致里面的listview高度不能确定,所以只能在程序中动态设置代码如下:

     1 public class Utility {  
     2        public static void setListViewHeightBasedOnChildren(ListView listView) {  
     3            ListAdapter listAdapter = listView.getAdapter();   
     4            if (listAdapter == null) {  
     5                // pre-condition  
     6                return;  
     7            }  
     8   
     9            int totalHeight = 0;  
    10            for (int i = 0; i < listAdapter.getCount(); i++) {  
    11                View listItem = listAdapter.getView(i, null, listView);  
    12                listItem.measure(0, 0);  
    13                totalHeight += listItem.getMeasuredHeight();  
    14            }  
    15   
    16            ViewGroup.LayoutParams params = listView.getLayoutParams();  
    17            params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1)); 
    18            listView.setLayoutParams(params);  
    19        }  
    20    } 

    只要在设置ListView的Adapter后调用此静态方法即可让ListView正确的显示在其父ListView的ListItem中。

    但是要注意的是,子ListView的每个Item必须是LinearLayout,不能是其他的,因为其他的Layout(如RelativeLayout)没有重写onMeasure(),所以会在onMeasure()时抛出异常。

    这样就可以实现scrollview加listview的 嵌套复杂布局了。

  • 相关阅读:
    gulp.js基础入门
    正则匹配
    app的meta标签
    移动端UC浏览器和QQ浏览器的部分私有meta属性
    雅虎团队经验:网站页面性能优化的 34条黄金守则
    DOCTYPE声明作用及用法详解
    DOCTYPE 中xhtml 1.0和 html 4.01区别分析
    html doctype 作用介绍
    WebStorm
    JQuery的Ajax跨域请求原理概述及实例
  • 原文地址:https://www.cnblogs.com/androidsj/p/4432381.html
Copyright © 2011-2022 走看看