http://my.oschina.net/yaowen424/blog/533092
原 ExpandableListView(可展开的列表组件)的说明以及其用法
赞
0
![](https://i-blog.csdnimg.cn/blog_migrate/a1937beb2783455099b3656331fd1df5.jpeg)
摘要
在日常开发,有可能会遇到需要一些可以展开的列表,学过了jQuery都知道,jQuery很方便的就做出来了。但是,用Android的该怎么做呢?其实在我没有接触过ExpandableListView类之前,都是自定义控件继承于ListView的。然后,后来发现了Android也给我们提供了ExpandableListView类,极大的方便了我们开发;
摘要提到了,我之前自定义的控件继承于ListView。顾名思义,ExpandableListView是ListView的子类。它在普通的ListView的基础上进行了拓展,它把应用中的列表分为几组,每组又包含多个列表项。
ExpandableListView的用法和ListView非常像,只是其所显示的列表项应该由ExpandableListAdapter提供,下面是它的xml属性及说明:
然而,接下来是用事实说话了:
--------------------------------------------------------------------------------
1、该项目的布局文件非常简单,和ListView差不多,此处就不贴出代码了。
2、ExpandableListViewActivity.java关键代码如下:
(要定义一个全局的私有的listView变量)
1
2
3
4
|
listView = (ExpandableListView) findViewById(R.id.list);
MyExpandableListAdapter adapter =
new
MyExpandableListAdapter();
listView.setAdapter(adapter);
|
3、MyExpandableListAdapter代码如下:
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
|
public
class
MyExpandableListAdapter
implements
ExpandableListAdapter {
int
[] logos =
new
int
[]{
R.drawable.word,
R.drawable.excel,
R.drawable.email,
R.drawable.ppt
};
private
String[] armTypes =
new
String[]{
"WORD"
,
"EXCEL"
,
"EMAIL"
,
"PPT"
};
private
String[][] arms =
new
String[][]{
{
"文档编辑"
,
"文档排版"
,
"文档处理"
,
"文档打印"
},
{
"表格编辑"
,
"表格排版"
,
"表格处理"
,
"表格打印"
},
{
"收发邮件"
,
"管理邮箱"
,
"登录登出"
,
"注册绑定"
},
{
"演示编辑"
,
"演示排版"
,
"演示处理"
,
"演示打印"
},
};
@Override
public
void
registerDataSetObserver(DataSetObserver observer) {
}
@Override
public
void
unregisterDataSetObserver(DataSetObserver observer) {
}
@Override
public
int
getGroupCount() {
return
armTypes.length;
}
@Override
public
int
getChildrenCount(
int
groupPosition) {
return
arms[groupPosition].length;
}
@Override
public
Object getGroup(
int
groupPosition) {
return
armTypes[groupPosition];
}
@Override
public
Object getChild(
int
groupPosition,
int
childPosition) {
return
arms[groupPosition][childPosition];
}
@Override
public
long
getGroupId(
int
groupPosition) {
return
groupPosition;
}
@Override
public
long
getChildId(
int
groupPosition,
int
childPosition) {
return
childPosition;
}
@Override
public
boolean
hasStableIds() {
return
true
;
}
@Override
public
View getGroupView(
int
groupPosition,
boolean
isExpanded, View convertView, ViewGroup parent) {
LinearLayout ll =
new
LinearLayout(ExpandableListViewActivity.
this
);
ll.setOrientation(LinearLayout.HORIZONTAL);
ImageView logo =
new
ImageView(ExpandableListViewActivity.
this
);
logo.setImageResource(logos[groupPosition]);
logo.setPadding(
36
,
15
,
0
,
0
);
ll.addView(logo);
TextView textView = getTextView();
textView.setText(getGroup(groupPosition).toString());
textView.setPadding(
10
,
0
,
0
,
0
);
ll.addView(textView);
return
ll;
}
@Override
public
View getChildView(
int
groupPosition,
int
childPosition,
boolean
isLastChild, View convertView, ViewGroup parent) {
TextView textView = getTextView();
textView.setText(getChild(groupPosition, childPosition).toString());
return
textView;
}
@Override
public
boolean
isChildSelectable(
int
groupPosition,
int
childPosition) {
return
true
;
}
@Override
public
boolean
areAllItemsEnabled() {
return
false
;
}
@Override
public
boolean
isEmpty() {
return
false
;
}
@Override
public
void
onGroupExpanded(
int
groupPosition) {
}
@Override
public
void
onGroupCollapsed(
int
groupPosition) {
}
@Override
public
long
getCombinedChildId(
long
groupId,
long
childId) {
return
0
;
}
@Override
public
long
getCombinedGroupId(
long
groupId) {
return
0
;
}
private
TextView getTextView() {
AbsListView.LayoutParams lp =
new
AbsListView.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
64
);
TextView textView =
new
TextView(ExpandableListViewActivity.
this
);
textView.setLayoutParams(lp);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
textView.setPadding(
36
,
0
,
0
,
0
);
textView.setTextSize(
20
);
return
textView;
}
}
|
代码上面没有做多少注释,在这里说明一下吧:
该adapter有两个关键方法:getChildView()和getGroupView();
----------前者返回的view对象作为子列表项,后者返回的view作为组列表项。
4、下面是该程序的运行界面了:
5、 就到这儿了结束吧, 谢谢观看。