
这是一个层级列表的源码,可以无限打造层级深度
1.Node类
package com.example.utils;
import java.util.ArrayList;
import java.util.List;
public class Node {
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<Node> children = new ArrayList<Node>();
public Node(int id, int pid, String name) {
this.id = id;
this.pid = pid;
this.name = name;
}
public Node() {
}
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 getLevel() {
return parent == null ? 0 : parent.getLevel() + 1;
}
public void setLevel(int level) {
this.level = level;
}
public boolean isExpand() {
return isExpand;
}
public void setExpand(boolean expand) {
isExpand = expand;
if (!isExpand){
for (Node node : children){
node.setExpand(false);
}
}
}
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<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
//是否为根节点
public boolean isRoot(){
return parent == null;
}
//判断父节点的收缩状态
public boolean isParentExpand(){
if (parent ==null){
return false;
}else {
return parent.isExpand();
}
}
//判断是否是叶节点
public boolean isLeaf(){
return children.size() == 0;
}
}
2.TreeHelper类
package com.example.utils;
import com.example.annotation.TreeNodeId;
import com.example.annotation.TreeNodeLabel;
import com.example.annotation.TreeNodepId;
import com.example.threedemo.R;
import java.io.File;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
public class ThreeHelper {
//将用户数据转化为树形数据
public static <T>List<Node> conver2Datas2Nodes(List<T> datas) throws IllegalAccessException {
List<Node> nodes = new ArrayList<>();
Node node = null;
for (T t : datas){
int id= -1;
int pid = -1;
String lable = null;
node

本文介绍了一个用于构建无限层级树形结构的Node类和辅助类TreeHelper,通过这些类可以将数据转换成树形结构,实现节点的展开和折叠功能。
最低0.47元/天 解锁文章
4377





