前言:
最近一个多月在认真的学习Android和做项目,文章内容表达的不好或者理解错了,希望大家评论指出。 :-)
本文是总结几个比较常用且使用的技巧,和一个大家都会遇到的问题。
文章中大部分语句摘抄自一下两篇大神写的文章:(如果对ExpandableListView一无所知,建议按照顺序去阅读,遇到问题再看本文)
1、Android中ExpandableListView的使用
网址:http://blog.csdn.net/gyflyx/article/details/6461242
2、[Android UI设计]ExpandableListView详解
网址:http://www.tuicool.com/articles/JjaMnqf
ExpandableListView是Android中可以实现下拉ListView的一个控件,是ListView的子类。
直接上图,就是这么一功能~
(点击就会展开,再点击就缩回去)
Android自带的布局不是这样的,这个是自定义了Group和Child的布局。
ExpandableListView的使用步骤:
1、在xml中定义一个ExpandableListView
2、在类中定义两个List集合,用于存放Group/Child中的内容,并初始化内容
3、定义ExpandableListView的Adapter,继承BaseExpanableListAdapter
例如:public class MyExpandableAdapter extends BaseExpandableListAdapter
4、最后给定义好的ExpandableView添加上Adapter
这里就贴出两个子布局的代码和MyExpandableAdapter的代码~
expandlist_group.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="70dp">
<RelativeLayout android:layout_width="match_parent" android:layout_height="55dp" android:background="#d1d1d1" android:layout_marginTop="15dp">
<TextView android:id="@+id/tv_group_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="30dp" android:text="小猫" android:textColor="#ffffff" android:textSize="20sp" android:gravity="center_vertical"/>
<ImageView android:id="@+id/iv_arrow" android:layout_width="25dp" android:layout_height="25dp" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:layout_marginRight="20dp" android:background="@drawable/right_arrow"/>
</RelativeLayout>
</LinearLayout>
expandlist_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="45dp">
<RelativeLayout android:layout_width="match_parent" android:layout_height="45dp" android:layout_marginLeft="10dp" android:layout_marginRight="10dp" android:background="#b3b3b3">
<TextView android:id="@+id/tv_child_name" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_marginLeft="30dp" android:text="声音1" android:textColor="#ffffff" android:textSize="20sp" android:gravity="center_vertical"/>
<ImageView android:id="@+id/iv_sound" android:layout_width="25dp" android:layout_height="25dp" android:layout_a