官方给出的解释是:A view that shows items in a vertically scrolling two-level list. This differs from the ListView by allowing two levels: groups which can individually be expanded to show its children. The items come from the ExpandableListAdapter associated with this view.
简单翻译一下就是:
一种用于垂直滚动展示两级列表的视图,组可以单独展开。这些选项的数据是通ExpandableListAdapter 关联的。
2.2.1ExpandableListView的属性(分割线,箭头等等)
三.属性
divider 这个属性用于设置父选项之间的分割线样式
childDivider 自选项之间分割样式
dividerHeight 用于设置分割线的高度
groupIndicator 这个属性是用于控制父项前面的小箭头用的,如果想要用自己的图片替换掉那个箭头可以这样写:
android:groupIndicator="@drawable/indicator"
android:groupIndicator="@drawable/indicator"
indicator.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_expanded="true" android:drawable="@drawable/open"/>
<item android:drawable="@drawable/close"/>
</selector>
<?xmlversion="1.0"encoding="utf-8"?>
2
<selectorxmlns:android="http://schemas.android.com/apk/res/android">
3
<itemandroid:state_expanded="true"android:drawable="@drawable/open"/>
4
<itemandroid:drawable="@drawable/close"/>
5
</selector>
indicatorLeft箭头或者自己设置的图片的左边框距离手机左边边缘的距离,类似于marginLeft
indicatorStart箭头或者自己设置的图片的左边框距离手机左边边缘的距离,类似于marginLeft
indicatorRight和indicatorEnd暂时还用不上,因为箭头一般在列表的左边,使用方法应该和上面两个方法差不多
childIndicator用于设置子项前显示的图标,不设置的话默认是没有图标的
2.3 ExpandableListView使用步骤,使用方法
2.3.1ExpandableListView的使用步骤
2.3.2ExpandableListView的使用方法
一.使用
1.定义布局文件
<ExpandableListView
android:id="@+id/expand_lv"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<ExpandableListView
2
android:id="@+id/expand_lv"
3
android:layout_width="match_parent"
4
android:layout_height="match_parent"/>
2.3.3ExpandableListView的集合数据讲解
2.定义要显示的数据
//group数据
private ArrayList<String> mGroupList;
//item数据
private ArrayList<ArrayList<String>> mItemSet;
private void initData() {
mGroupList = new ArrayList<>();
mGroupList.add("我的家人");
mGroupList.add("我的朋友");
mGroupList.add("黑名单");
mItemSet = new ArrayList<>();
ArrayList<String> itemList1 = new ArrayList<>();
itemList1.add("大妹");
itemList1.add("二妹");
itemList1.add("三妹");
ArrayList<String> itemList2 = new ArrayList<>();
itemList2.add("大美");
itemList2.add("二美");
itemList2.add("三美");
ArrayList<String> itemList3 = new ArrayList<>();
itemList3.add("狗蛋");
itemList3.add("二丫");
mItemSet.add(itemList1);
mItemSet.add(itemList2);
mItemSet.add(itemList3);
}26
1
//group数据
2
privateArrayList<String>mGroupList;
3
//item数据
4
privateArrayList<ArrayList<String>>mItemSet;
5
privatevoidinitData() {
6
mGroupList=newArrayList<>();
7
mGroupList.add("我的家人");
8
mGroupList.add("我的朋友");
9
mGroupList.add("黑名单");
10
11
mItemSet=newArrayList<>();
12
ArrayList<String>itemList1=newArrayList<>();
13
itemList1.add("大妹");
14
itemList1.add("二妹");
15
itemList1.add("三妹");
16
ArrayList<String>itemList2=newArrayList<>();
17
itemList2.add("大美");
18
itemList2.add("二美");
19
itemList2.add("三美");
20
ArrayList<String>itemList3=newArrayList<>();
21
itemList3.add("狗蛋");
22
itemList3.add("二丫");
23
mItemSet.add(itemList1);
24
mItemSet.add(itemList2);
25
mItemSet.add(itemList3);
26
}
3.定义组视图和子选项的视图
组视图
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_centerVertical="true"
android:layout_marginLeft="50dp"
android:id="@+id/tv_group"
android:textSize="18sp"
android:text="name"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="4/18"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>23
1
<?xmlversion="1.0"encoding="utf-8"?>
2
<RelativeLayout
3
xmlns:android="http://schemas.android.com/apk/res/android"
4
android:layout_width="match_parent"
5
android:layout_height="50dp">
6
<TextView
7
android:layout_centerVertical="true"
8
android:layout_marginLeft="50dp"
9
android:id="@+id/tv_group"
10
android:textSize="18sp"
11
android:text="name"
12
android:layout_width="wrap_content"
13
android:layout_height="wrap_content"/>
14
15
<TextView
16
android:layout_centerVertical="true"
17
android:layout_alignParentRight="true"
18
android:layout_marginRight="10dp"
19
android:text="4/18"
20
android:textSize="16sp"
21
android:layout_width="wrap_content"
22
android:layout_height="wrap_content"/>
23
</RelativeLayout>
子视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_centerVertical="true"
android:padding="8dp"
android:layout_height="wrap_content">
<ImageView
android:layout_marginLeft="40dp"
android:src="@mipmap/ic_launcher"
android:layout_width="40dp"
android:layout_height="40dp" />
<LinearLayout
android:layout_marginLeft="12dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tv_name"
android:text="@string/app_name"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:text="@string/app_name"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>31
1
<?xmlversion="1.0"encoding="utf-8"?>
2
<LinearLayout
3
xmlns:android="http://schemas.android.com/apk/res/android"
4
android:layout_width="match_parent"
5
android:layout_centerVertical="true"
6
android:padding="8dp"
7
android:layout_height="wrap_content">
8
<ImageView
9
android:layout_marginLeft="40dp"
10
android:src="@mipmap/ic_launcher"
11
android:layout_width="40dp"
12
android:layout_height="40dp"/>
13
<LinearLayout
14
android:layout_marginLeft="12dp"
15
android:orientation="vertical"
16
android:layout_width="wrap_content"
17
android:layout_height="wrap_content">
18
<TextView
19
android:id="@+id/tv_name"
20
android:text="@string/app_name"
21
android:textSize="16sp"
22
android:layout_width="wrap_content"
23
android:layout_height="wrap_content"/>
24
<TextView
25
android:text="@string/app_name"
26
android:textSize="16sp"
27
android:layout_width="wrap_content"
28
android:layout_height="wrap_content"/>
29
</LinearLayout>
30
31
</LinearLayout>
2.4BaseExpandableListAdapter适配器的介绍,主要方法介绍使用
2.4.1BaseExpandableListAdapter介绍
2.4.2 BaseExpandableListAdapter主要方法介绍
4.自定义适配器,需要继承 BaseExpandableListAdapter 抽象类,重写相关的方法
public class MyAdapter extends BaseExpandableListAdapter {
private Context mContext;
private ArrayList<String> mGroup;
private ArrayList<ArrayList<String>> mItemList;
private final LayoutInflater mInflater;
public MyAdapter(Context context, ArrayList<String> group, ArrayList<ArrayList<String>> itemList){
this.mContext = context;
this.mGroup = group;
this.mItemList = itemList;
mInflater = LayoutInflater.from(context);
}
//父项的个数
@Override
public int getGroupCount() {
return mGroup.size();
}
//某个父项的子项的个数
@Override
public int getChildrenCount(int groupPosition) {
return mItemList.get(groupPosition).size();
}
//获得某个父项
@Override
public Object getGroup(int groupPosition) {
return mGroup.get(groupPosition);
}
//获得某个子项
@Override
public Object getChild(int groupPosition, int childPosition) {
return mItemList.get(groupPosition).get(childPosition);
}
//父项的Id
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//子项的id
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
//获取父项的view
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = mInflater.inflate(R.layout.item_group,parent,false);
}
String group = mGroup.get(groupPosition);
TextView tvGroup = (TextView) convertView.findViewById(R.id.tv_group);
tvGroup.setText(group);
return convertView;
}
//获取子项的view
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String child = mItemList.get(groupPosition).get(childPosition);
if (convertView == null){
convertView = mInflater.inflate(R.layout.item_item,parent,false);
}
TextView tvChild = (TextView)convertView.findViewById(R.id.tv_name);
tvChild.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(mContext,child,Toast.LENGTH_SHORT).show();
}
});
tvChild.setText(child);
return convertView;
}
//子项是否可选中,如果要设置子项的点击事件,需要返回true
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}93
1
publicclassMyAdapterextendsBaseExpandableListAdapter{
2
3
privateContextmContext;
4
privateArrayList<String>mGroup;
5
privateArrayList<ArrayList<String>>mItemList;
6
privatefinalLayoutInflatermInflater;
7
8
publicMyAdapter(Contextcontext,ArrayList<String>group,ArrayList<ArrayList<String>>itemList){
9
this.mContext=context;
10
this.mGroup=group;
11
this.mItemList=itemList;
12
mInflater=LayoutInflater.from(context);
13
}
14
15
//父项的个数
16
@Override
17
publicintgetGroupCount() {
18
returnmGroup.size();
19
}
20
21
//某个父项的子项的个数
22
@Override
23
publicintgetChildrenCount(intgroupPosition) {
24
returnmItemList.get(groupPosition).size();
25
}
26
27
//获得某个父项
28
@Override
29
publicObjectgetGroup(intgroupPosition) {
30
returnmGroup.get(groupPosition);
31
}
32
33
//获得某个子项
34
@Override
35
publicObjectgetChild(intgroupPosition,intchildPosition) {
36
returnmItemList.get(groupPosition).get(childPosition);
37
}
38
39
//父项的Id
40
@Override
41
publiclonggetGroupId(intgroupPosition) {
42
returngroupPosition;
43
}
44
45
//子项的id
46
@Override
47
publiclonggetChildId(intgroupPosition,intchildPosition) {
48
returnchildPosition;
49
}
50
51
@Override
52
publicbooleanhasStableIds() {
53
returnfalse;
54
}
55
56
//获取父项的view
57
@Override
58
publicViewgetGroupView(intgroupPosition,booleanisExpanded,ViewconvertView,ViewGroupparent) {
59
if(convertView==null){
60
convertView=mInflater.inflate(R.layout.item_group,parent,false);
61
}
62
Stringgroup=mGroup.get(groupPosition);
63
TextViewtvGroup=(TextView)convertView.findViewById(R.id.tv_group);
64
tvGroup.setText(group);
65
returnconvertView;
66
}
67
68
//获取子项的view
69
@Override
70
publicViewgetChildView(intgroupPosition,intchildPosition,booleanisLastChild,ViewconvertView,ViewGroupparent) {
71
finalStringchild=mItemList.get(groupPosition).get(childPosition);
72
if(convertView==null){
73
convertView=mInflater.inflate(R.layout.item_item,parent,false);
74
}
75
76
TextViewtvChild=(TextView)convertView.findViewById(R.id.tv_name);
77
tvChild.setOnClickListener(newView.OnClickListener() {
78
@Override
79
publicvoidonClick(Viewv) {
80
Toast.makeText(mContext,child,Toast.LENGTH_SHORT).show();
81
}
82
});
83
tvChild.setText(child);
84
returnconvertView;
}
87
88
//子项是否可选中,如果要设置子项的点击事件,需要返回true
@Override
publicbooleanisChildSelectable(intgroupPosition,intchildPosition) {
returntrue;
}
}
5.为 ExpandableListView 设置适配器
MyAdapter adapter = new MyAdapter(this, mGroupList, mItemSet);
mExpandableListView.setAdapter(adapter);
MyAdapteradapter=newMyAdapter(this,mGroupList,mItemSet);
mExpandableListView.setAdapter(adapter);
2.4.3ExpandableListView的条目点击监听和条目长安监听
二.监听事件
对于处理 Item 的点击事件,还是要设置监听器,常用的有这几类:
setOnChildClickListener//单击子选项
setOnGroupClickListener//单击组选项
setOnGroupCollapseListener//分组合并
setOnGroupExpandListener//分组合并
注意:
setOnChildClickListener,想要此回调有效,适配器的isChildSelectable()方法必须返回true