ExpandableListView的用法

ExpandableListView组件是android中一个比较常用的组件,当点击一个父item的时候可以将它的子item显示出来,像手机QQ中的好友列表就是实现的类型效果。使用ExpandableListView组件的关键就是设置它的adapter,这个adapter必须继承BaseExpandbaleListAdapter类,所以实现运用ExpandableListView的核心就是学会继承这个BaseExpanableListAdapter类。

下面是一个小demo:

activity_main.xml:

1
2
3
4
5
6
7
8
9
10
11
12
<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" >

<ExpandableListView
android:id="@+id/main_expandablelistview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</RelativeLayout>

layout_parent.xml:(父item运用的样式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/parent_textview"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:gravity="center" />

</LinearLayout>

layout_children.xml:(子item运用的样式)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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="match_parent"
android:orientation="vertical" >

<TextView
android:id="@+id/second_textview"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="center"
android:gravity="center" />

</LinearLayout>

MainActivity.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package com.example.android_expandablelistview;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

public class MainActivity extends Activity {

ExpandableListView mainlistview = null;
List<String> parent = null;
Map<String, List<String>> map = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainlistview = (ExpandableListView) this
.findViewById(R.id.main_expandablelistview);

initData();
mainlistview.setAdapter(new MyAdapter());
}

// 初始化数据
public void initData() {
parent = new ArrayList<String>();
parent.add("parent1");
parent.add("parent2");
parent.add("parent3");

map = new HashMap<String, List<String>>();

List<String> list1 = new ArrayList<String>();
list1.add("child1-1");
list1.add("child1-2");
list1.add("child1-3");
map.put("parent1", list1);

List<String> list2 = new ArrayList<String>();
list2.add("child2-1");
list2.add("child2-2");
list2.add("child2-3");
map.put("parent2", list2);

List<String> list3 = new ArrayList<String>();
list3.add("child3-1");
list3.add("child3-2");
list3.add("child3-3");
map.put("parent3", list3);

}

class MyAdapter extends BaseExpandableListAdapter {

//得到子item需要关联的数据
@Override
public Object getChild(int groupPosition, int childPosition) {
String key = parent.get(groupPosition);
return (map.get(key).get(childPosition));
}

//得到子item的ID
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}

//设置子item的组件
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
String key = MainActivity.this.parent.get(groupPosition);
String info = map.get(key).get(childPosition);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_children, null);
}
TextView tv = (TextView) convertView
.findViewById(R.id.second_textview);
tv.setText(info);
return tv;
}

//获取当前父item下的子item的个数
@Override
public int getChildrenCount(int groupPosition) {
String key = parent.get(groupPosition);
int size=map.get(key).size();
return size;
}
//获取当前父item的数据
@Override
public Object getGroup(int groupPosition) {
return parent.get(groupPosition);
}

@Override
public int getGroupCount() {
return parent.size();
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
//设置父item组件
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) MainActivity.this
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.layout_parent, null);
}
TextView tv = (TextView) convertView
.findViewById(R.id.parent_textview);
tv.setText(MainActivity.this.parent.get(groupPosition));
return tv;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}

}
}

最后的实现结果:


ExpandableListView 设置默认展开

1、先实例化一个ExpandableListView对象,例如mExpandableListView

2、然后mExpandableListView.setAdapter(exlvAdapter);

3、遍历所有group,将所有项设置成默认展开

1
2
3
4
int groupCount = mExpandableListView.getCount();
for (int i=0; i<groupCount; i++) {
mExpandableListView.expandGroup(i);
};