数据处理的工具类:
public class TreeDatasHelperTools {
/**
* 将用户提供的数据转化成树层级上可用数据
* @description:
* @date 2015-10-9 下午4:07:24
*/
public static <T> List<Node> datas2Nodes(List<T> datas) throws IllegalAccessException, IllegalArgumentException {
List<Node> nodes = new ArrayList<Node>();
Node node = null;
for (T t : datas) {
int id = -1;
int pid = -1;
String label = "";
// node = new Node();
Class clazz = t.getClass();
Field[] fields = clazz.getDeclaredFields();// 反射获取类中的字段
for (Field field : fields) {
if (field.getAnnotation(TreeNodeId.class) != null) {// 根据字段上的注解来获取对应的值
field.setAccessible(true);// 在java的反射使用中,如果字段是私有的,那么必须要对这个字段设置才能正常使用,否则报错
id = field.getInt(t);
}
if (field.getAnnotation(TreeNodePid.class) != null) {
field.setAccessible(true);
pid = field.getInt(t);
}
if (field.getAnnotation(TreeNodeLabel.class) != null) {
field.setAccessible(true);
label = (String) field.get(t);
}
}
node = new Node(id, pid, label);
nodes.add(node);
}
// 设置nodes中的父子节点关系
for (int i = 0; i < nodes.size(); i++) {
Node n = nodes.get(i);
for (int j = i + 1; j < nodes.size(); j++) {
Node m = nodes.get(j);
if (m.getPid() == n.getId()) {// 如果m的父节点pid==n的id,则m是父节点,n是子节点
n.getChildren().add(m);
m.setParent(n);
}
else if (m.getId() == n.getPid()) {
m.getChildren().add(n);
n.setParent(m);
}
}
}
// 设置节点图片
for (Node n : nodes) {
setNodeIcon(n);
}
return nodes;
}
/**
* 为我们的node数据设置对应的图标
* @description:
* @date 2015-10-9 下午4:46:29
*/
private static void setNodeIcon(Node n) {
if (n.getChildren().size() > 0 && n.isExpand()) {// 如果有子节点且展开状态
n.setIcon(R.drawable.icon_unable);
}
else if (n.getChildren().size() > 0 && !n.isExpand()) {
n.setIcon(R.drawable.icon_enable);
}
else {
n.setIcon(-1);
}
}
public static <T> List<Node> getSortedNodes(List<T> datas, int defaultExpandLevel) throws IllegalAccessException, IllegalArgumentException {
List<Node> result = new ArrayList<Node>();
List<Node> nodes = datas2Nodes(datas);
// 首先获取根节点数据
List<Node> rootNodes = getRootNodes(nodes);
for (Node node : rootNodes) {
addNode(result, node, defaultExpandLevel, 1);
}
return result;
}
/**
* 获取数据中的所有根节点数据
* @description:
* @date 2015-10-9 下午5:00:32
*/
private static List<Node> getRootNodes(List<Node> nodes) {
List<Node> root = new ArrayList<Node>();
for (Node node : nodes) {
if (node.isRoot()) {
root.add(node);
}
}
return root;
}
/**
* 获取到可见的节点数据
* @description:
* @date 2015-10-9 下午5:12:58
*/
public static List<Node> filterVisibleNodes(List<Node> mAllNodes) {
List<Node> nodes = new ArrayList<Node>();
for (Node node : mAllNodes) {
if (node.isRoot() || node.isParentExpand()) {
setNodeIcon(node);
nodes.add(node);
}
}
return nodes;
}
/**
* 把一个节点的所有子节点都放入result中
* @description:
* @date 2015-10-9 下午5:05:57
*/
private static void addNode(List<Node> result, Node node, int defaultExpandLevel, int currentLevel) {
result.add(node);
if (defaultExpandLevel >= currentLevel) {
node.setExpand(true);
}
if (node.isLeaf()) { return; }
for (int i = 0; i < node.getChildren().size(); i++) {
addNode(result, node.getChildren().get(i), defaultExpandLevel, currentLevel + 1);
}
}
}










