3.设置节点间的关联关系
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())
{
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;
}
public static List getSortedNodes(List datas,
int defaultExpandLevel) throws IllegalArgumentException
,
IllegalAccessException
{
List result = new ArrayList();
List nodes = convertDatas2Nodes(datas);
// 获得树的根结点
List rootNodes = getRootNodes(nodes);
for (Node node : rootNodes)
{
addNode(result, node, defaultExpandLevel, 1);
}
Log.e("TAG", result.size() + "");
return result;
}
private static void addNode(List 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);
}
}
4.排序以及过滤出要显示的数据
public static List filterVisibleNodes(List nodes)
{
List result = new ArrayList();
for (Node node : nodes)
{
if (node.isRoot() || node.isParentExpand())
{
setNodeIcon(node);
result.add(node);
}
}
return result;
}
private static List getRootNodes(List nodes)
{
List root = new ArrayList();
for (Node node : nodes)
{
if (node.isRoot())
{
root.add(node);
}
}
return root;
}
private static void setNodeIcon(Node n)
{
if (n.getChildren().size() > 0 && n.isExpand())
{
n.setIcon(R.drawable.tree_ex);
} else if (n.getChildren().size() > 0 && !n.isExpand())
{
n.setIcon(R.drawable.tree_ec);
} else
{
n.setIcon(-1);
}
}
4.排序以及过滤出要显示的数据