参考过下面的非常好的博客,写了个treeView
https://blog.csdn.net/lmj623565791/article/details/40212367
但是美中不足的是:
1. 这个treeview只能查,无法修改(比如删除节点等)
本实现就是对上述博文的一个改进。
原理分析:
listview是android原生非常强大的展示list的view;我的实现就是基于这个listview。
和listview不同的是:
1. 如果点击树形结构的非叶子节点,其节点需要展开或者缩放
2. 根据节点的层次,需要进行合理的indent显示
因此,要实现上述功能的核心:改造listview对应的adapter。当点击节点/删除节点或者增加节点时,动态调整adapter对应的数据内容。
本实现包含了3个类,Node、Tree和TreeListViewApdter。
Node: 用来保存每个节点的信息;
Tree用来保存整个数据结构。
TreeListViewApdter继承BaseAdapter,用于listView的Adapter
具体来看看这个三个类的内容:
Node
public class Node <T>{
private int _id; //节点id
private Node _parent; //父节点
private List<Node> _children=new ArrayList<>(); //所有的儿子节点
private T obj; //依附这个节点的用户对象
private int _size_all; //本节点对应的树的大小(计算其下所有的节点,无论展开状态是啥)
private int _size_expand; //本节点对应的树展开状态大小
private boolean isExpand=true; //本节点的展开状态
public Node(int id, T obj){
this._id = id;
this.obj = obj;
_size_expand=1;
_size_all=1;
}
Node get_parent(){
return _parent;}
void set_parent(Node node){
this._parent = node;
}
List<Node> get_children(){
return _children;}
int get_size(boolean isExpand){
return isExpand?_size_expand:_size_all;}
void set_size(int size,boolean isExpand){
if(isExpand)
_size_expand = size;
else
_size_all=size;
}
//在当前这个node为根的树上寻找等于id的node,如果找不到,返回null
//isExpand:
//true:表示在expand树上查找
// false: 表示不考虑expand属性,在整颗树上找
Node find_Node_By_Id(int id,boolean isExpand){
if(this.get_id()==id)
return this;
List<Node> list=this.get_children();
if(list.size()==0){
return null;
}else{
if((isExpand && this.getExpand()) || !isExpand){
for(Node node: this.get_children()){
Node result=node.find_Node_By_Id(id,isExpand);
if(result!=null)
return result;
}
}
}
return null;
}
//按照深度优先,遍历以本节点为根的整个树,返回第position个元素的node
//position从0开始
//isExpand:
//true:表示在expand树上查找
// false: 表示不考虑expand属性,在整颗树上找
Node get(int position,boolean isExpand){
if(position==0)
return this;
position--;
List<Node> list=this.get_children();
if(list.size()==0){
return null;
}else{
if(!isExpand || (isExpand && this.getExpand())){
for(Node node:this.get_children()){
int size = position - node.get_size(isExpand);
if(size<0){
return node.get(position,isExpand);
}else{
position=size;
}
}