放置比较少的ListView组件效果图:

                       

Item布局文件  list_view_item.xml  布局文件的内容:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="50dp"
     >

<ImageView
        android:id="@+id/list_view_img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="5dip"
        android:padding="5dip" />

<TextView
        android:id="@+id/list_view_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:gravity="center_vertical|center_horizontal"
        android:layout_marginLeft="50dip"
        android:text="@string/title_listview_activity"
        android:textColor="#000"
        android:textSize="16dp"
        tools:context=".ListViewActivity" />
    
  <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="260dip"
        android:src="@drawable/img_go"
        android:padding="5dip" />
</RelativeLayout>

在Activity界面中放置ListView时,ListView组件比较少时,不需要滑动,这几个ListView组件都可以正常显示,当放置多时就不能如我们所愿,我们可以使用ScrollView组件来嵌套ListView组件这样就可正常显示了,原理就是  我们在代码中动态设置ListView组件的高度,下面我们来看srollView视图中嵌套ListView组件实现的方法:

/*
 * 动态设置ListView组建的高度
 * 
 * */
public void setListViewHeightBasedOnChildren(ListView listView) {
      
      ListAdapter listAdapter = listView.getAdapter();
      
      if (listAdapter == null) {
      
       return;
      
      }
      
      int totalHeight = 0;
      
      for (int i = 0; i < listAdapter.getCount(); i++) {
      
       View listItem = listAdapter.getView(i, null, listView);
      
       listItem.measure(0, 0);
      
       totalHeight += listItem.getMeasuredHeight();
      
      }
      
      ViewGroup.LayoutParams params = listView.getLayoutParams();
      
      params.height = totalHeight
      
        + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
      
      // params.height += 5;// if without this statement,the listview will be
      
      // a
      
      // little short
      
      // listView.getDividerHeight()获取子项间分隔符占用的高度
      
      // params.height最后得到整个ListView完整显示需要的高度
      
      listView.setLayoutParams(params);
      
    }

在为ListView组件的实例设置适配器时候,要对组件高度进行动态设置,调用上面的 setListViewHeightBasedOnChildren(listView)方法即可:

l istView.setAdapter(adapter);
        setListViewHeightBasedOnChildren(listView);
        listView1.setAdapter(adapter1);
        setListViewHeightBasedOnChildren(listView1);
        listView2.setAdapter(adapter2);

setListViewHeightBasedOnChildren(listView2);

动态配置ListView组件高度以后,效果图上方  第二张片所示!!!!!!

ScrollView中嵌套ListView相关推荐

  1. scrollview中嵌套listview产生冲突问题

    在scrollview中嵌套listview或者gridview时 如果listview或者gridview的高度时wrap-content会导致两者只显示一行多一点 原因: listview和gri ...

  2. Android之解决在scrollview中嵌套ListView切换界面时scrollview整体向下滑动

    有时候我们在scrollview中会嵌套ListView,在嵌套ListView(ListView上面还有一些控件)后,切换Activity时会发现整个 scrollview 被向下滑动直到ListV ...

  3. ScrollView中嵌套ListView控件,数据无法显示完全

    为什么80%的码农都做不了架构师?>>>    public void setListViewHeightBasedOnChildren(ListView listView) { / ...

  4. 【Android】ListView、RecyclerView、ScrollView里嵌套ListView 相对优雅的解决方案:NestFullListView

    一 背景概述: ScrollView里嵌套ListView,一直是Android开发者(反正至少是我们组)最讨厌的设计之一,完美打破ListView(RecyclerView)的复用机制,成功的将Na ...

  5. [转载]Android: 如何实现ScrollView中含有ListView?

    原文地址:Android: 如何实现ScrollView中含有ListView?作者:mailofzxf ListView本身就含有ScrollView,因此把ListView放到ScrollView ...

  6. ScrollView中嵌入Listview,当item高度不一样的时候,item展示不全问题

    首先在scrollview中嵌入listview,我们需要动态设置listview的高度,否则只会展示一行. public void fixListViewHeight(ListView listVi ...

  7. android之ScrollView里嵌套ListView(转)

    hi,大家好,研究完ScrollView嵌套ScrollView之后,本人突然又想研究ScrollView里嵌套ListView了. 如果还不知道ScrollView嵌套ScrollView是怎么实现 ...

  8. ScrollView中嵌套recycleView 出现的不显示,显示不全,终极解决方案

    ScrollView中嵌套recycleView 出现的不显示,显示不全,终极解决方案 参考文章: (1)ScrollView中嵌套recycleView 出现的不显示,显示不全,终极解决方案 (2) ...

  9. ScrollView中嵌套WebView底部留白问题解决

    ScrollView中嵌套WebView出现底部滑动留有大面积空白问题网络上有各种解决方案. 本博文基本上能终结该问题的出现,下面是网络上解决步骤: 1:第一种解决方案也是最多人提到的,在加载完成后重 ...

最新文章

  1. 【GCN】图卷积网络 Graph Convolutional Networks
  2. python中单个下划线是什么意思
  3. 009_CSS分组选择器
  4. 准备入门IC的全局观念系列-中
  5. python 深拷贝_Python之类的浅拷贝与深拷贝
  6. 快速排序算法_常用排序算法专题—快速排序
  7. vnc移植_VNC远程控制软件使用说明
  8. SAP ABAP实用技巧介绍系列之 ABAP XSLT copy keyword
  9. 亿图图示11.0新版本全新上线 助力个人及企业降本增效
  10. JAVA面试总结(初版)
  11. vmware卸载时提示“The MSI failed”解决方法
  12. LATEX参考文献添加文章doi号并嵌入超链接+IEEE期刊缩写查询
  13. 新浪微博开放平台中的Redis实践
  14. ORAN专题系列-29:运营商O-RAN扩展皮站测试的硬件架构
  15. 宽带多天线无线传输技术发展展望
  16. php 解析mpp 格式文件
  17. markdown 转word
  18. (PTA)6-9 字符串压缩
  19. 关于5G时延的深度解读,非常详尽
  20. 张晓飞ajax聊天室,张晓飞

热门文章

  1. 【C 语言】指针数据类型 ( 野指针 | 避免野指针推荐方案 )
  2. 【错误记录】PyCharm 运行 Python 程序报错 ( PEP 8: W292 no newline at end of file )
  3. 【Java 虚拟机原理】Class 字节码二进制文件分析 二 ( 常量池位置 | 常量池结构 | tag | info[] | 完整分析字节码文件中的常量池二进制数据 )
  4. 【Android 应用开发】Android 杀进程总结 ( 杀后台进程 | 杀前台进程 | 杀其它进程 )
  5. 51NOD 1001 数组中和等于K的数对
  6. myeclise 安装
  7. BZOJ 1856: [Scoi2010]字符串 [Catalan数]
  8. DataBase project physical design
  9. python学习笔记---字符串
  10. ArcGIS Engine开发前基础知识(4)