您的位置:首页 > 其它

ExpandableListView 和CheckBox结合使用

2016-06-14 15:19 302 查看
先说说需求,这样便于理解代码:(以图来讲解说明)

这是A页面:可以看到一个医疗组:组名是从B 页面拿到的。想要更换组名,点击这个组,就跳转到B页面。



2.这是B页面:页面是分组选项,其中:A页面上次选的组名要在B页面展示出来,同时,用户也可以重新选择组名,再传递给A.



3.做的过程中碰到的难点就是:A页面选择的组名怎么在B页面中展示。

(1) 我刚开始的思路是:把A页面选择的组名的ID保留传递给B,然后和B中所有的组名ID比较,如果有找到,说明是选中状态,就把CheckBox 设置为true. 所以在BaseExpandableListAdapter 的继承类中,重写getGroupView的时候,有以下代码来比较ID.

//初始化特定的checkbox状态:
updateCheckBoxView(mGrouplist,cb_parent,mGroupId,groupPosition);
notifyDataSetChanged();


private void updateCheckBoxView(ArrayList<DoctorGroupBean> mGrouplist, CheckBox cbParent, String mGroupId, int groupPosition) {

if(mGrouplist!=null){
String roomgroupid = mGrouplist.get(groupPosition).getROOMGROUPID();
if(roomgroupid.equals(mGroupId)){
cbParent.setChecked(true);
mGrouplist.get(groupPosition).setIsChecked(true);
}else{
cbParent.setChecked(false);
mGrouplist.get(groupPosition).setIsChecked(false);
}

}

}


(2)初始化CheckBox的状态之后,用户也可能重新选择组名,这个需要个listener. 于是在getGroupView中再加Checkbox的监听:

CompoundButton.OnCheckedChangeListener listener = new MyOnCheckedChangeListener(mGrouplist, groupPosition);
cb_parent.setOnCheckedChangeListener(listener);


@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

//获取的是组是否有标记:
DoctorGroupBean doctorGroupBean = mGrouplist.get(groupPosition);
if (isChecked) {
//设置特定的组的状态是true:

doctorGroupBean.setIsChecked(true);

//除去特定的组,其余组都是false
for (int i = 0; i < mGrouplist.size(); i++) {
if (i != groupPosition) {
mGrouplist.get(i).setIsChecked(false);
}
}
notifyDataSetChanged();
//返回到上一个页面:
returnBack();

} else {
//设置组的状态是false:未选中
doctorGroupBean.setIsChecked(false);
notifyDataSetChanged();
}

}


(3)运行之后,发现checkbox的监听好像不起作用。原来选中的没有被取消,新选中的也没有改变。问题就出在我传递过来的ID上. 虽然做了监听,但是在notify之后,ID还是固定不变的。于是想办法改变下ID,listener改用匿名内部类,便于改变ID.

cb_parent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
//mGrouplist.get(groupPosition).setIsChecked(true);
//在这里将选中的组名的id传递给mGroupId(mGroupId就是上个页面传递过来的) ,这样id就跟着改变了。
mGroupId = mGrouplist.get(groupPosition).getROOMGROUPID();
notifyDataSetChanged();
returnBack();
}else{
// mGrouplist.get(groupPosition).setIsChecked(false);
notifyDataSetChanged();
}
}
});


(4)将选中的组名的信息返回给上个页面:我也不需要给这个对象设置是否选中的属性,只要判断组名ID是否为mGroupId,如果是,则将这个组返回:

/**
* 将选中的组的信息返回给上个页面提供的方法
*
* @param mGrouplist
* @return
*/
public DoctorGroupBean getCheckedGroup(ArrayList<DoctorGroupBean> mGrouplist) {
//mGrouplist 是组名集合
DoctorGroupBean newBean = new DoctorGroupBean();
if (mGrouplist != null && mGrouplist.size() > 0) {
for (int i = 0; i < mGrouplist.size(); i++) {
//这是组名:
DoctorGroupBean doctorGroupBean = mGrouplist.get(i);
boolean isChecked = doctorGroupBean.getIsChecked();
if (doctorGroupBean.getROOMGROUPID()== mGroupId) {
//newBean.setIsChecked(isChecked);
newBean.setROOMGROUP_NAME(doctorGroupBean.getROOMGROUP_NAME());
newBean.setMEMBERLIST(doctorGroupBean.getMEMBERLIST());
newBean.setROOMGROUPID(doctorGroupBean.getROOMGROUPID());
break;

}
}
}
return newBean;

}


经过以上步骤的修正,现在可以将A页面选中的组名在B页面展示,同时,用户改变组名,也能监听到并返回给A页面。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息