android中常常要用到ListView,有时也要用到ExpandableListView,如在手机设置中,对于分类有很好的效果,会用ListView的人一定会用ExpandableListView,因为
ExpandableListView extends ListView的,下面来看个简单的例子
运行效果图:
ExpandableListView extends ListView的,下面来看个简单的例子
运行效果图:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<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/expendlist"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
>
</ExpandableListView>
</RelativeLayout>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
//扩展ListView的头List布局文件
<?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=
"wrap_content"
android:orientation=
"horizontal"
>
<TextView
android:id=
"@+id/txt"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
<ImageView
android:id=
"@+id/img"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
//扩展ListView的子List布局文件
<?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=
"horizontal"
android:padding=
"10.0dp"
>
<ImageView
android:id=
"@+id/img"
android:layout_width=
"20.0dp"
android:layout_height=
"20.0dp"
/>
<TextView
android:id=
"@+id/txt"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
/>
</LinearLayout>
|
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
145
146
147
148
149
150
151
152
153
|
package
com.example.test;
import
java.util.ArrayList;
import
java.util.List;
import
android.app.Activity;
import
android.content.Context;
import
android.os.Bundle;
import
android.view.View;
import
android.view.ViewGroup;
import
android.widget.BaseExpandableListAdapter;
import
android.widget.ExpandableListView;
import
android.widget.ImageView;
import
android.widget.TextView;
public
class
MainActivity
extends
Activity {
private
ExpandableListView expandableListView;
private
List<String> group_list;
private
List<List<String>> item_list;
private
List<List<Integer>> item_list2;
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//随便一堆测试数据
group_list =
new
ArrayList<String>();
group_list.add(
"A"
);
group_list.add(
"B"
);
group_list.add(
"C"
);
item_list =
new
ArrayList<List<String>>();
item_list.add(group_list);
item_list.add(group_list);
item_list.add(group_list);
List<Integer> tmp_list =
new
ArrayList<Integer>();
tmp_list.add(R.drawable.ic_launcher);
tmp_list.add(R.drawable.ic_launcher);
tmp_list.add(R.drawable.ic_launcher);
item_list2 =
new
ArrayList<List<Integer>>();
item_list2.add(tmp_list);
item_list2.add(tmp_list);
item_list2.add(tmp_list);
expandableListView = (ExpandableListView) findViewById(R.id.expendlist);
expandableListView.setAdapter(
new
MyExpandableListViewAdapter(
this
));
}
//用过ListView的人一定很熟悉,只不过这里是BaseExpandableListAdapter
class
MyExpandableListViewAdapter
extends
BaseExpandableListAdapter {
private
Context context;
public
MyExpandableListViewAdapter(Context context) {
this
.context = context;
}
@Override
public
int
getGroupCount() {
return
group_list.size();
}
@Override
public
int
getChildrenCount(
int
groupPosition) {
return
item_list.get(groupPosition).size();
}
@Override
public
Object getGroup(
int
groupPosition) {
return
group_list.get(groupPosition);
}
@Override
public
Object getChild(
int
groupPosition,
int
childPosition) {
return
item_list.get(groupPosition).get(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) {
GroupHolder groupHolder =
null
;
if
(convertView ==
null
) {
convertView = (View) getLayoutInflater().from(context).inflate(
R.layout.expendlist_group,
null
);
groupHolder =
new
GroupHolder();
groupHolder.txt = (TextView) convertView.findViewById(R.id.txt);
// groupHolder.img = (ImageView) convertView
// .findViewById(R.id.img);
convertView.setTag(groupHolder);
}
else
{
groupHolder = (GroupHolder) convertView.getTag();
}
groupHolder.txt.setText(group_list.get(groupPosition));
return
convertView;
}
@Override
public
View getChildView(
int
groupPosition,
int
childPosition,
boolean
isLastChild, View convertView, ViewGroup parent) {
ItemHolder itemHolder =
null
;
if
(convertView ==
null
) {
convertView = (View) getLayoutInflater().from(context).inflate(
R.layout.expendlist_item,
null
);
itemHolder =
new
ItemHolder();
itemHolder.txt = (TextView) convertView.findViewById(R.id.txt);
itemHolder.img = (ImageView) convertView.findViewById(R.id.img);
convertView.setTag(itemHolder);
}
else
{
itemHolder = (ItemHolder) convertView.getTag();
}
itemHolder.txt.setText(item_list.get(groupPosition).get(
childPosition));
itemHolder.img.setBackgroundResource(item_list2.get(groupPosition).get(
childPosition));
return
convertView;
}
@Override
public
boolean
isChildSelectable(
int
groupPosition,
int
childPosition) {
return
true
;
}
}
class
GroupHolder {
public
TextView txt;
public
ImageView img;
}
class
ItemHolder {
public
ImageView img;
public
TextView txt;
}
}
|