Android仿QQ好友列表实现列表收缩与展开

2019-12-10 19:33:12王旭
易采站长站为您分析Android仿QQ好友列表实现列表收缩与展开,感兴趣的小伙伴们可以参考一下  

ExpandableListView是一个垂直滚动显示两级列表项的视图,与ListView不同的是,它可以有两层:每一层都能够被独立的展开并显示其子项。
好友QQ列表,可以展开,可以收起,在android中,以往用的比较多的是listview,虽然可以实现列表的展示,但在某些情况下,我们还是希望用到可以分组并实现收缩的列表,那就要用到android的ExpandableListView,今天研究了一下这个的用法,也参考了很多资料动手写了一个小demo,实现了基本的功能,下面直接上效果图以及源代码~!
本文效果:

Android仿QQ好友列表实现列表收缩与展开

Android仿QQ好友列表实现列表收缩与展开

一、实现原理
1、首先必须在布局文件中定义一个ExpandableListView
2、其次创建一级条目对应的布局文件group
3、创建二级条目对应的布局文件child
4、加载ExpandableListView组件的Activity必须继承自ExpandableListActivity
二、布局与代码
1、首先在主布局中activity_main.xml

<LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
  android:orientation="vertical" > 
 
  <ExpandableListView 
    android:id="@id/android:list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" /> 
 
</LinearLayout> 

2、其次在drawable文件夹定义布局一级列表groups.xml

<LinearLayout 
  xmlns:android="http://www.easck.com/apk/res/android" 
  android:orientation="vertical" 
  android:layout_width="fill_parent" 
  android:layout_height="fill_parent" 
> 
  <TextView 
    android:id="@+id/textGroup" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:paddingLeft="40px" 
    android:paddingTop="6px" 
    android:paddingBottom="6px" 
    android:textSize="25sp" 
    android:text="No data" 
  /> 
 
</LinearLayout>