1.前言
ListView类控件在APP中经常使用,本文主要介绍ExpandableListView,完成两级列表显示的效果。这一节(上篇、下篇)主要介绍如何仿照QQ,以及ListView和ExpandableListView的主要知识点,实现一个好友列表(静态的数据,下一节介绍如何网络上动态获取数据);
2.实现效果如下:
实现要点:在于如何布局ChildView,以及如何定制ExpandableListView的子view, 图片资源,可以破解QQ.apk文件获取,本文末尾也会附上图片资源的;
3.实现步骤1:新建主Activity布局文件,layout/activity_expandable_list_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${packageName}.${activityClass}" >
<TextView
android:id="@+id/txt_list_title"
android:layout_width="match_parent"
android:layout_height="33dp"
android:background="@drawable/groupbar_last_bg_press"
android:gravity="center_vertical"
android:text="好友分组"
android:paddingLeft="8dip"
android:textSize="12sp" />
<ExpandableListView
android:id="@+id/explist_friends"
android:layout_width="match_parent"
android:layout_height="0dp"
android:groupIndicator="@drawable/expandable_group_bg"
android:layout_weight="1"
>
</ExpandableListView>
</LinearLayout>
4.实现步骤2:新建ExpandableListView的GroupItem,ChildItem布局文件
GroupItem布局文件:layout/expandablelist_group_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="30dip"
android:layout_gravity="center"
android:gravity="center_vertical"
android:background="@drawable/groupbar_bg"
android:orientation="horizontal">
<TextView android:id="@+id/txt_group_title"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="36dip"
android:text="我的分组"
android:gravity="left"/>
<TextView android:id="@+id/txt_group_num"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="0/1"
android:paddingRight="8dip"
android:g