Android ExpandableListView简单例子

来源:互联网 发布:js数组合并排序 编辑:程序博客网 时间:2024/06/06 05:15
   android中常常要用到ListView,有时也要用到ExpandableListView,如在手机设置中,对于分类有很好的效果,会用ListView的人一定会用ExpandableListView,因为 
 ExpandableListView extends ListView的,下面来看个简单的例子 
  
 运行效果图: 
  

Android ExpandableListView简单例子
?
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
packagecom.example.test;
 
 importjava.util.ArrayList;
 importjava.util.List;
 
 importandroid.app.Activity;
 importandroid.content.Context;
 importandroid.os.Bundle;
 importandroid.view.View;
 importandroid.view.ViewGroup;
 importandroid.widget.BaseExpandableListAdapter;
 importandroid.widget.ExpandableListView;
 importandroid.widget.ImageView;
 importandroid.widget.TextView;
 
 publicclass MainActivity extendsActivity {
 
 privateExpandableListView expandableListView;
 privateList<String> group_list;
 privateList<List<String>> item_list;
 privateList<List<Integer>> item_list2;
 
 @Override
 protectedvoid onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
 
  //随便一堆测试数据
  group_list = newArrayList<String>();
  group_list.add("A");
  group_list.add("B");
  group_list.add("C");
 
  item_list = newArrayList<List<String>>();
  item_list.add(group_list);
  item_list.add(group_list);
  item_list.add(group_list);
 
  List<Integer> tmp_list = newArrayList<Integer>();
  tmp_list.add(R.drawable.ic_launcher);
  tmp_list.add(R.drawable.ic_launcher);
  tmp_list.add(R.drawable.ic_launcher);
 
  item_list2 = newArrayList<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(newMyExpandableListViewAdapter(this));
 }
 
 //用过ListView的人一定很熟悉,只不过这里是BaseExpandableListAdapter
 classMyExpandableListViewAdapter extendsBaseExpandableListAdapter {
 
  privateContext context;
 
  publicMyExpandableListViewAdapter(Context context) {
   this.context = context;
  }
 
  @Override
  publicint getGroupCount() {
   returngroup_list.size();
  }
 
  @Override
  publicint getChildrenCount(intgroupPosition) {
   returnitem_list.get(groupPosition).size();
  }
 
  @Override
  publicObject getGroup(intgroupPosition) {
   returngroup_list.get(groupPosition);
  }
 
  @Override
  publicObject getChild(intgroupPosition, intchildPosition) {
   returnitem_list.get(groupPosition).get(childPosition);
  }
 
  @Override
  publiclong getGroupId(intgroupPosition) {
   returngroupPosition;
  }
 
  @Override
  publiclong getChildId(intgroupPosition, intchildPosition) {
   returnchildPosition;
  }
 
  @Override
  publicboolean hasStableIds() {
   returntrue;
  }
 
  @Override
  publicView getGroupView(intgroupPosition, booleanisExpanded,
    View convertView, ViewGroup parent) {
   GroupHolder groupHolder = null;
   if(convertView == null) {
    convertView = (View) getLayoutInflater().from(context).inflate(
      R.layout.expendlist_group, null);
    groupHolder = newGroupHolder();
    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));
   returnconvertView;
  }
 
  @Override
  publicView getChildView(intgroupPosition, intchildPosition,
    booleanisLastChild, View convertView, ViewGroup parent) {
   ItemHolder itemHolder = null;
   if(convertView == null) {
    convertView = (View) getLayoutInflater().from(context).inflate(
      R.layout.expendlist_item, null);
    itemHolder = newItemHolder();
    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));
   returnconvertView;
  }
 
  @Override
  publicboolean isChildSelectable(intgroupPosition, intchildPosition) {
   returntrue;
  }
 
 }
 
 classGroupHolder {
  publicTextView txt;
  publicImageView img;
 }
 
 classItemHolder {
  publicImageView img;
  publicTextView txt;
 }
}
0 0
原创粉丝点击