- 浏览: 705113 次
文章分类
- 全部博客 (324)
- 图片 (1)
- Javascript (25)
- Android (144)
- css (6)
- intellij (3)
- jquery (9)
- java (29)
- mysql (5)
- grails (1)
- groovy (1)
- Android Eclipse (2)
- linux (12)
- web (5)
- 算法 (2)
- 软件安装 (1)
- dedecms (12)
- centsos (6)
- tomcat (1)
- win 2003 (3)
- 杰奇 (1)
- window (1)
- loadrunner (3)
- QTP (38)
- VBS (33)
- Android 自动化 (12)
- python (3)
最新评论
-
masuweng:
Intellij 中javax.servlet.http.HttpServlet包导不进来 -
臧殿彬:
为什么我在主线程中定义也会出现这个bug?
java.lang.RuntimeException: Can't create handler inside thread that has not cal -
wpp1993:
...
http请求返回并解析json数据 -
javasea:
不错不错
socket阻塞与非阻塞,同步与异步、I/O模型 -
suyuwen1:
Apache2.4.x版wampserver本地php服务器如 ...
WAMPserver配置(允许外部访问、phpmyadmin设置为输入用户名密码才可登录等)
ExpandableList 跟 ListView 的實作觀念一樣
可以用 SimpleExpandableListAdapter 跟繼承 BaseExpandableListAdapter 的自訂 Adapter
由於 ExpandableList 有二層的資料
所以在 Adapter 要分別設定 Group 與 Child 的資料
實作是比較複雜一點
如果要在 ExpandableList 的 Group 及 Child 分別加上 CheckBox
而且 Group 的 CheckBox 跟 Child 的 CheckBox 互相關聯的話
在自訂 Adapter 就必須注意幾點
- 在 getGroupView 函式必須設定 CheckBox 監聽 Click 事件,並且綁定實作自訂的 Click 事件
- 在自訂的 Click 事件裡,改變原始資料後,呼叫 notifyDataSetChanged 函式,通知 ExpandableList 重新產生畫面
activity_main.xml
1
2
3
4
5
|
< ExpandableListView
android:id = "@+id/listView"
android:layout_width = "match_parent"
android:layout_height = "wrap_content" >
</ ExpandableListView >
|
group_layout.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
< TextView
android:id = "@+id/tvGroup"
android:layout_width = "280dp"
android:layout_height = "45dip"
android:gravity = "center_vertical|left"
android:paddingLeft = "45dp"
android:textSize = "17dip"
android:textStyle = "bold" >
</ TextView >
< CheckBox
android:id = "@+id/chbGroup"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:focusable = "false" />
|
group_layout.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
< TextView
android:id = "@+id/tvChild"
android:layout_width = "280dp"
android:layout_height = "45dip"
android:gravity = "center_vertical"
android:paddingLeft = "45dp"
android:textSize = "17dip"
android:textStyle = "bold" >
</ TextView >
< CheckBox
android:id = "@+id/chbChild"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:focusable = "false" />
|
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
|
@Override public void onCreate(Bundle savedInstanceState) {
super .onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
groups = new ArrayList<Group>();
getJSONObject();
listView = (ExpandableListView) findViewById(R.id.listView);
adapter = new EListAdapter( this , groups);
listView.setAdapter(adapter);
} /** 解悉 JSON 字串 */ private void getJSONObject() {
String jsonStr = "{'CommunityUsersResult':[{'CommunityUsersList':[{'fullname':'a111','userid':11,'username':'a1'}"
+ ",{'fullname':'b222','userid':12,'username':'b2'}],'id':1,'title':'人事部'},{'CommunityUsersList':[{'fullname':"
+ "'c333','userid':13,'username':'c3'},{'fullname':'d444','userid':14,'username':'d4'},{'fullname':'e555','userid':"
+ "15,'username':'e5'}],'id':2,'title':'開發部'},{'CommunityUsersList':[],'id':3,'title':'業務部'}]}" ;
try {
JSONObject CommunityUsersResultObj = new JSONObject(jsonStr);
JSONArray groupList = CommunityUsersResultObj.getJSONArray( "CommunityUsersResult" );
for ( int i = 0 ; i < groupList.length(); i++) {
JSONObject groupObj = (JSONObject) groupList.get(i);
Group group = new Group(groupObj.getString( "id" ), groupObj.getString( "title" ));
JSONArray childrenList = groupObj.getJSONArray( "CommunityUsersList" );
for ( int j = 0 ; j < childrenList.length(); j++) {
JSONObject childObj = (JSONObject) childrenList.get(j);
Child child = new Child(childObj.getString( "userid" ), childObj.getString( "fullname" ), childObj.getString( "username" ));
group.addChildrenItem(child);
}
groups.add(group);
}
} catch (JSONException e) {
Log.d( "allenj" , e.toString());
}
} |
EListAdapter.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
|
public class EListAdapter extends BaseExpandableListAdapter {
private Context context;
private ArrayList<Group> groups;
public EListAdapter(Context context, ArrayList<Group> groups) {
this .context = context;
this .groups = groups;
}
public Object getChild( int groupPosition, int childPosition) {
groups.get(groupPosition).getChildItem(childPosition);
return null ;
}
public long getChildId( int groupPosition, int childPosition) {
return childPosition;
}
public int getChildrenCount( int groupPosition) {
return groups.get(groupPosition).getChildrenCount();
}
public Object getGroup( int groupPosition) {
return groups.get(groupPosition);
}
public int getGroupCount() {
return groups.size();
}
public long getGroupId( int groupPosition) {
return groupPosition;
}
public boolean hasStableIds() {
return false ;
}
public boolean isChildSelectable( int groupPosition, int childPosition) {
return false ;
}
/** 設定 Group 資料 */
public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
Group group = (Group) getGroup(groupPosition);
if (convertView == null ) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_layout, null );
}
TextView tv = (TextView) convertView.findViewById(R.id.tvGroup);
tv.setText(group.getTitle());
// 重新產生 CheckBox 時,將存起來的 isChecked 狀態重新設定
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.chbGroup);
checkBox.setChecked(group.getChecked());
// 點擊 CheckBox 時,將狀態存起來
checkBox.setOnClickListener( new Group_CheckBox_Click(Integer.valueOf(groupPosition)));
return convertView;
}
/** 勾選 Group CheckBox 時,存 Group CheckBox 的狀態,以及改變 Child CheckBox 的狀態 */
class Group_CheckBox_Click implements OnClickListener {
private int groupPosition;
Group_CheckBox_Click( int groupPosition) {
this .groupPosition = groupPosition;
}
public void onClick(View v) {
groups.get(groupPosition).toggle();
// 將 Children 的 isChecked 全面設成跟 Group 一樣
int childrenCount = groups.get(groupPosition).getChildrenCount();
boolean groupIsChecked = groups.get(groupPosition).getChecked();
for ( int i = 0 ; i < childrenCount; i++)
groups.get(groupPosition).getChildItem(i).setChecked(groupIsChecked);
// 注意,一定要通知 ExpandableListView 資料已經改變,ExpandableListView 會重新產生畫面
notifyDataSetChanged();
}
}
/** 設定 Children 資料 */
public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
Child child = groups.get(groupPosition).getChildItem(childPosition);
if (convertView == null ) {
LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout, null );
}
TextView tv = (TextView) convertView.findViewById(R.id.tvChild);
tv.setText(child.getFullname());
// 重新產生 CheckBox 時,將存起來的 isChecked 狀態重新設定
CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.chbChild);
checkBox.setChecked(child.getChecked());
// 點擊 CheckBox 時,將狀態存起來
checkBox.setOnClickListener( new Child_CheckBox_Click(Integer.valueOf(groupPosition), Integer.valueOf(childPosition)));
return convertView;
}
/** 勾選 Child CheckBox 時,存 Child CheckBox 的狀態 */
class Child_CheckBox_Click implements OnClickListener {
private int groupPosition;
private int childPosition;
Child_CheckBox_Click( int groupPosition, int childPosition) {
this .groupPosition = groupPosition;
this .childPosition = childPosition;
}
public void onClick(View v) {
groups.get(groupPosition).getChildItem(childPosition).toggle();
// 檢查 Child CheckBox 是否有全部勾選,以控制 Group CheckBox
int childrenCount = groups.get(groupPosition).getChildrenCount();
boolean childrenAllIsChecked = true ;
for ( int i = 0 ; i < childrenCount; i++) {
if (!groups.get(groupPosition).getChildItem(i).getChecked())
childrenAllIsChecked = false ;
}
groups.get(groupPosition).setChecked(childrenAllIsChecked);
// 注意,一定要通知 ExpandableListView 資料已經改變,ExpandableListView 會重新產生畫面
notifyDataSetChanged();
}
}
} |
Group.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
|
public class Group {
private String id;
private String title;
private ArrayList<Child> children;
private boolean isChecked;
public Group(String id, String title) {
this .id = id;
this .title = title;
children = new ArrayList<Child>();
this .isChecked = false ;
}
public void setChecked( boolean isChecked) {
this .isChecked = isChecked;
}
public void toggle() {
this .isChecked = ! this .isChecked;
}
public boolean getChecked() {
return this .isChecked;
}
public String getId() {
return id;
}
public String getTitle() {
return title;
}
public void addChildrenItem(Child child) {
children.add(child);
}
public int getChildrenCount() {
return children.size();
}
public Child getChildItem( int index) {
return children.get(index);
}
} |
Group.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
|
public class Child {
private String userid;
private String fullname;
private String username;
private boolean isChecked;
public Child(String userid, String fullname, String username) {
this .userid = userid;
this .fullname = fullname;
this .username = username;
this .isChecked = false ;
}
public void setChecked( boolean isChecked) {
this .isChecked = isChecked;
}
public void toggle() {
this .isChecked = ! this .isChecked;
}
public boolean getChecked() {
return this .isChecked;
}
public String getUserid() {
return userid;
}
public String getFullname() {
return fullname;
}
public String getUsername() {
return username;
}
} |
注意事項
SimpleExpandableListAdapter 呼叫 notifyDataSetChanged 函式時
是在 New 出一個 Adapter 時,用 adapter.notifyDataSetChanged()
不過自訂 Adapter 的話,要在自訂 Adapter 類別裡呼叫 notifyDataSetChanged() 即可
這一點筆者也是卡了很久
转载:http://www.allenj.net/?p=3644
发表评论
-
将博客搬至CSDN
2014-12-24 22:28 826将博客搬至CSDN -
[Android设计模式]Android退出应用程序终极方法
2014-01-08 16:41 644首先,我们将管理Activity的功能通过一个扩展的Appl ... -
android中activity的四种加载模式
2014-01-08 16:20 912一、何为加载模式 在android的多activit ... -
Android实现数据存储技术
2014-01-08 13:49 861本文介绍Android中的5种数据存储方式。 数据存 ... -
-Android各版本系统源代码下载
2014-01-07 16:39 888原文地址http://blog.csdn.net/wangj ... -
XmlPullParser解析xml文件
2014-01-07 15:55 23931XML解析三种方式 DOM 通用性强,它会将XML文 ... -
ListView实现二级菜单
2014-01-06 18:53 3008实现如上图所示的二级菜单: 1上面是通过两个ListVi ... -
三种方式实现自定义圆形进度条ProgressBar
2014-01-06 16:46 3339一、通过动画实现 定义res/anim/loading.x ... -
android屏幕适配不同的资源图片、布局
2014-01-06 16:35 11551、 资源图片的适配: 在android ... -
获取屏幕分辨率 及 dp 和 像素关系
2014-01-06 16:24 1778DisplayMetrics metric = new Di ... -
Android用Application设置全局变量以及使用
2014-01-03 16:44 780如果想在整个应用中使用全局变量,在java中一般是使用静态变 ... -
Android SDK各版本源码
2013-06-07 20:47 1380http://repository.grepcode.com ... -
在android系统中增加预编译apk(use BUILD_PREBUILT instead!. Stop)
2013-06-05 22:55 103061,在commom.mk(build/target/prod ... -
android out目录结构
2013-06-02 14:37 1474Android编译完成后,将 ... -
Android OpenGL ES 开发教程 从入门到精通
2013-05-25 09:17 1034Android OpenGL ES 简明开发教程 An ... -
自定义ContentProvider
2013-05-20 19:10 1469自定义ContentProvider,根据不同的条件查询不同 ... -
Broadcast Receiver(广播接收器)
2013-05-19 23:11 1057Braodcast Receiver顾名思义就是广播接收器, ... -
Android Service生命周期及用法!
2013-05-19 22:22 1056来自:http://blog.csdn.net/andro ... -
Androdi Droid Fu介绍
2013-05-19 10:01 1163Droid-Fu是啥 Droid ... -
自定义dialog 含listview(二)
2013-05-16 12:13 2210弹出一个半透明的listview dialog: publ ...
相关推荐
Android中ListView自定义Item的Checkbox
style="@style/CheckBoxStyles"//这里就是用户可以自定CheckBox的样式 android:layout_width="match_parent" android:layout_height="100dip" /> 2:举例:可如下定义 在styles.xml添加这个样式的配置 ...
最近公司需求做个员工报餐界面用到多个CheckBox 行列
CircleCheckBox Screenshot: Attrs attr format ...CircleCheckBox is available in the MavenCentral, so getting it as simple as adding it as a ...checkBox.setListener(new CircleCheckBox.On
自学 Android 做的第五个 Android 程序,使用了 CheckBox
拥有三种状态纯Material风格的Android Checkbox控件
Android 带有切换动画的CheckBox
Android里ListView里面添加CheckBox实现多选,用上ViewHolder和ConvertView
Android 长按Listview显示CheckBox,实现批量删除,具体功能请参考: http://blog.csdn.net/ddxxll2008/article/details/49125759
android CheckBox的使用简单实例。
本demo实现功能为:Expandablelistview子项checkBox先获取已选择状态,在次过程中可修改子项选择,最终提交选择子类的项,并打印相关数据
这是我自己写的android listview里面使用checkbox的demo
用Android写的树形结构多选CheckBox工具,代码简单,拿来就可以使用,这是我优化后可以直接当工具使用的DEMO。
ExpandableListView的group和child里面都含有CheckBox框,二者联动。
Android完美解决ListView复用导致的Checkbox状态混乱问题,具体见博客 http://www.jianshu.com/p/744dcca03a99
在Android中使用Radio和Checkbox的源码例子
Android中Selsetor选中时改变CheckBox背景,在开发中非常常用,我们通常会用到单选框,多选框,在实际项目中,默认的颜色不会满足我们的要求,所以,我们需要自己设置,这是我写的一个例子希望能和大家交流学习
简单实现自定义checkbox控件四种选择状态的图片样式
android checkbox 的isChecked属性
NULL 博文链接:https://justwyy.iteye.com/blog/1531106