1.创建节点Node
public class Node
{
public Node()
{
}
public Node(int id, int pId, String name)
{
this.id = id;
this.pId = pId;
this.name = name;
}
private int id;
private int pId = 0;
private String name;
private int level;
private boolean isExpand = false;
private int icon;
private Node parent;
private List children = new ArrayList();
public int getId()
{
return id;
}
public void setId(int id)
{
this.id = id;
}
public int getpId()
{
return pId;
}
public void setpId(int pId)
{
this.pId = pId;
}
public String getName()
{
return name;
}
public void setName(String name)
{
this.name = name;
}
public int getIcon()
{
return icon;
}
public void setIcon(int icon)
{
this.icon = icon;
}
public Node getParent()
{
return parent;
}
public void setParent(Node parent)
{
this.parent = parent;
}
public List getChildren()
{
return children;
}
public void setChildren(List children)
{
this.children = children;
}
public boolean isRoot()
{
return parent == null;
}
public boolean isParentExpand()
{
if (parent == null)
return false;
return parent.isExpand();
}
public boolean isLeaf()
{
return children.size() == 0;
}
public int getLevel()
{
return parent == null ? 0 : parent.getLevel() + 1;
}
public boolean isExpand()
{
return isExpand;
}
public void setExpand(boolean isExpand)
{
this.isExpand = isExpand;
if(!isExpand)
{
for(Node node :children)
{
node.setExpand(false);
}
}
}
public void setLevel(int level)
{
this.level = level;
}
@Override
public String toString()
{
return "Node [id=" + id + ", pId=" + pId + ", name=" + name
+ ", level=" + level + ", isExpand=" + isExpand + ", icon="
+ icon + "]";
}
}
2.将用户的数据转化成根节点Node:用反射+注解
public static List convertDatas2Nodes(List datas)
throws IllegalArgumentException
, IllegalAccessException
{
List nodes = new ArrayList();
Node node = null;
for (T t : datas)
{
int id = -1;
int pid = -1;
String label = null;
node = new Node();
Class clazz = t.getClass();
Field[] fields = clazz.getDeclaredFields();
for (Field field : fields)
{
if (field.getAnnotation(TreeNodeId.class) != null)
{
field.setAccessible(true);
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);
}// for end
Log.e("TAG", nodes+"");
package com.imooc.treeview.utils.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeNodeId
{
// Class type();
}
package com.imooc.treeview.utils.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeNodeLabel
{
}
package com.imooc.treeview.utils.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface TreeNodePid
{
}