转载请注明本文出自Xzhi的博客(http://blog.csdn.net/CSDN_zhi/article/details/61200010),请尊重他人的辛勤劳动成果,谢谢!
前言
过几天就要开始物联网比赛了,从百忙之中抽出一点时间总结下在备赛中遇到ExpandableListView的使用。之前也遇到过ExpandableListView,但只是使用SimpleExpandableListAdapter适配器简单的实现。SimpleExpandableListAdapter虽然使用简单,但不适合一些复杂的功能,获取数据源也不方便。比如,我在前天备赛训练中遇到这样的麻烦:Android端从PC端获取数据并以ExpandableListView形式展示,这时候的数据源就不是死的了,SimpleExpandableListAdapter是也可以实现但本人感觉还是相当的麻烦。这就需要用到BaseExpandableListAdapter适配器了。
效果图
我就简单使用BaseExpandableListAdapter实现下仿QQ添加分组功能。
先看下效果图:
xml代码
直接上代码
activity_main.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="match_parent"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/add"
android:layout_width="60dp"
android:layout_height="60dp"
android:onClick="add"
android:src="@drawable/add" />
<ExpandableListView
android:id="@+id/expandableListView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/add"
>
</ExpandableListView>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/add"
android:layout_marginBottom="20dp"
android:layout_toRightOf="@+id/add"
android:text="添加分组" />
</RelativeLayout>
child_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/cName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/imageView1"
android:layout_marginBottom="32dp"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/imageView1"
android:text="TextView" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="80dp"
android:layout_height="90dp"
android:layout_a