今天移动开发课程验收完成,可以小小的松一口气了,感觉在同学中,自己做的这个计步器还是挺不错的嘛~~
然后对原来的手机计步器(pedometer)项目又做了一些完善:
注:这应该是本系列第三篇?
先给出ExpandableListView的一些效果图
先给出ExpandableListViewAdapter的代码:
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
154
155
156
157
158
159
160
161
|
package com.bit.expandablelistview; import java.util.HashMap; import java.util.List; import android.content.Context; import android.view.LayoutInflater; 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 ExpandableListViewAdapter extends BaseExpandableListAdapter { private List<string> group; //这是组视图 private HashMap<string, user= "" >> child; //这是子视图 private ExpandableListView listView; //当选择另一个组视图时关闭当前打开的组视图 private LayoutInflater inflater; //加载布局文件 private int last; //判断是否打开当前的页面 public ExpandableListViewAdapter(Context context, List<string> group, HashMap<string, user= "" >> child, ExpandableListView listView) { this .group = group; this .child = child; this .listView = listView; this .inflater = LayoutInflater.from(context); } /** * 对外提供一个借口,当数据改变时调用这个函数 * @param group * @param child */ public void setOnDataChanged(List<string> group, HashMap<string, user= "" >> child) { this .group = group; this .child = child; this .notifyDataSetChanged(); } @Override public int getGroupCount() { // TODO Auto-generated method stub return group.size(); } @Override public int getChildrenCount( int groupPosition) { List<user> tempList = child.get(group.get(groupPosition)); if (tempList != null ) { return tempList.size(); } else { return 0 ; } } @Override public Object getGroup( int groupPosition) { // TODO Auto-generated method stub return group.get(groupPosition); } @Override public Object getChild( int groupPosition, int childPosition) { // TODO Auto-generated method stub return child.get(group.get(groupPosition)).get(childPosition); } @Override public long getGroupId( int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public long getChildId( int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public boolean hasStableIds() { // TODO Auto-generated method stub return false ; } /** * 得到组视图布局文件,对这个进行了优化,可以看到converView是一个Android提供的缓存的对象,如果第一次没有这个布局文件 * 就创建一个,如果有的话,直接从convertView得到,不用进行创建,减少内存开销 */ public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { String groupName = group.get(groupPosition); View view; TextView name; if (convertView == null ) { view = inflater.inflate(R.layout.group_item, null ); name = (TextView) view.findViewById(R.id.group_number); view.setTag(name); } else { view = convertView; name = (TextView) view.getTag(); } name.setText(groupName); return view; } private class ViewHolder { ImageView picture; TextView name; } /** * 得到子视图的布局文件,类似于组视图 */ @Override public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { User user = child.get(group.get(groupPosition)).get(childPosition); View view; ViewHolder viewHolder; if (convertView == null ) { view = inflater.inflate(R.layout.child_item, null ); viewHolder = new ViewHolder(); viewHolder.picture = (ImageView) view.findViewById(R.id.pic); viewHolder.name = (TextView) view.findViewById(R.id.child_name); view.setTag(viewHolder); } else { view = convertView; viewHolder = (ViewHolder) view.getTag(); } viewHolder.name.setText(user.getName()); viewHolder.picture.setImageResource(user.getPicture()); return view; } /** * 设置可以点击子视图 */ @Override public boolean isChildSelectable( int groupPosition, int childPosition) { // TODO Auto-generated method stub return true ; } @Override public void onGroupExpanded( int groupPosition) { super .onGroupExpanded(groupPosition); if (groupPosition != last) { listView.collapseGroup(last); } last = groupPosition; } } </user></string,></string></string,></string></string,></string> |
然后给出MainActivity类的代码
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
|
package com.bit.expandablelistview; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.ExpandableListView; import android.widget.ExpandableListView.OnChildClickListener; import android.widget.Toast; public class MainActivity extends Activity implements OnChildClickListener { private List<string> group; //这是组视图 private HashMap<string, user= "" >> child; //这是子视图 private ExpandableListView listView; //这是可以扩展的listview private ExpandableListViewAdapter adapter; //这是子视图要加载的所有图片 private int pic[] = new int [] { R.drawable.head1, R.drawable.head2, R.drawable.head3, R.drawable.head4, R.drawable.head5, R.drawable.head6, R.drawable.head7, R.drawable.head8, R.drawable.head9, R.drawable.head10 }; @Override protected void onCreate(Bundle savedInstanceState) { super .onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); prepareData(); showData(); } /** * 显示数据 */ private void showData() { if (adapter == null ) { adapter = new ExpandableListViewAdapter( this , group, child, listView); listView.setAdapter(adapter); } else { adapter.setOnDataChanged(group, child); } } /** * 初始化一些参数,设置监听事件 */ private void init() { group = new ArrayList<string>(); child = new HashMap<string, user= "" >>(); listView = (ExpandableListView) findViewById(R.id.expandlistview); listView.setOnChildClickListener( this ); } /** * 准备测试数据 */ private void prepareData() { group.add(第一组); group.add(第二组); group.add(第三组); List<user> users = new ArrayList<user>(); for ( int i = 0 ; i < 4 ; i++) { User user = new User(); user.setName(张全蛋 + i); user.setPicture(pic[i]); users.add(user); } child.put(group.get( 0 ), users); users = new ArrayList<user>(); for ( int i = 4 ; i < 7 ; i++) { User user = new User(); user.setName(李铁柱 + i); user.setPicture(pic[i]); users.add(user); } child.put(group.get( 1 ), users); users = new ArrayList<user>(); for ( int i = 7 ; i < 10 ; i++) { User user = new User(); user.setName(唐马儒 + i); user.setPicture(pic[i]); users.add(user); } child.put(group.get( 2 ), users); } @Override public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) { //点击显示当前子视图的位置 Toast.makeText( MainActivity. this , 你点击的是 + child.get(group.get(groupPosition)) .get(childPosition).getName(), Toast.LENGTH_SHORT).show(); return false ; } } </user></user></user></user></string,></string></string,></string> |
大家注意,其中的User类是自己定义的一个类。
相关文章:
基于Android的计步器(Pedometer)的讲解(一)——Fragment页面跳转 – Android移动开发技术文章_手机开发 – 红黑联盟
基于Android的计步器(Pedometer)的讲解(二)——柱状图分析 – Android移动开发技术文章_手机开发 – 红黑联盟
http://www.codeclip.com/3566.html
基于Android的计步器(Pedometer)的讲解(四)——后台记步 – Android移动开发技术文章_手机开发 – 红黑联盟
基于Android的计步器(Pedometer)的讲解(五)——跟随界面滑动的指示器 – Android移动开发技术文章_手机开发 – 红黑联盟
《基于Android的计步器(Pedometer)的讲解—ExpandableListView – Android移动开发技术文章_手机开发 – 红黑联盟》有2个想法
评论已关闭。