protected void setCheckedWithBean(final Node node, boolean checked, HashMap<Integer, DepartmentItemBean> itemBeanHashMap) {
node.setChecked(checked);
if (checked) {
itemBeanHashMap.put(((DepartmentItemBean) node.bean).id, (DepartmentItemBean) node.bean);
} else {
itemBeanHashMap.remove(((DepartmentItemBean) node.bean).id);
}
setChildChecked(node, checked, itemBeanHashMap);
if (node.getParent() != null)
setNodeParentChecked(node.getParent(), checked, itemBeanHashMap);
notifyDataSetChanged();
}
/**
* 选中子节点
*
* @param node
* @param checked
* @param <T>
* @param <B>
*/
public <T, B> void setChildChecked(Node<T, B> node, boolean checked, HashMap<Integer, DepartmentItemBean> itemBeanHashMap) {
if (!node.isLeaf()) {
node.setChecked(checked);
if (checked) {
itemBeanHashMap.put(((DepartmentItemBean) node.bean).id, (DepartmentItemBean) node.bean);
} else {
itemBeanHashMap.remove(((DepartmentItemBean) node.bean).id);
}
for (Node childrenNode : node.getChildren()) {
setChildChecked(childrenNode, checked, itemBeanHashMap);
}
} else {
node.setChecked(checked);
if (checked) {
itemBeanHashMap.put(((DepartmentItemBean) node.bean).id, (DepartmentItemBean) node.bean);
} else {
itemBeanHashMap.remove(((DepartmentItemBean) node.bean).id);
}
}
}
private void setNodeParentChecked(Node node, boolean checked, HashMap<Integer, DepartmentItemBean> itemBeanHashMap) {
// 只向下影响
if (!checked) {
List<Node> childrens = node.getChildren();
boolean isChecked = false;
for (Node children : childrens) {
if (children.isChecked()) {
isChecked = true;
}
}
//如果所有自节点都没有被选中 父节点也不选中
if (!isChecked) {
node.setChecked(checked);
if (checked) {
itemBeanHashMap.put(((DepartmentItemBean) node.bean).id, (DepartmentItemBean) node.bean);
} else {
itemBeanHashMap.remove(((DepartmentItemBean) node.bean).id);
}
} else {
node.setChecked(checked);
if (checked) {
itemBeanHashMap.put(((DepartmentItemBean) node.bean).id, (DepartmentItemBean) node.bean);
} else {
itemBeanHashMap.remove(((DepartmentItemBean) node.bean).id);
}
}
if (node.getParent() != null) {
setNodeParentChecked(node.getParent(), checked, itemBeanHashMap);
}
}
}