app/src/main/java/com/application/zhangshi_app_android/widget/Node.java
@@ -96,7 +96,7 @@
        if (this.parent!=null){
            throw new RuntimeException("只能初始化根节点");
        }
        //根据可见性找到最后一个第一子节点,即最上边的节点
        //根据可见性找到最后一个第一子节点,即最右上边的节点
        setViewPositionRecursion(getVisibleTopNode(), this,x,y);
    }
@@ -116,12 +116,17 @@
        }
        //因为 y 是整颗节点树的y坐标,正常情况下,y都会是 顶部节点topNode 的y坐标
        //但是 有一种情况,就是顶部节点topNode刚好是根节点rootNode的唯一子节点,且topNode没有配偶,此时topNode的高度比rootNode的高度小,所以此时的y是rootNode的y坐标
        int centerY;
        if (rootNode.height >= topNode.height && rootNode == topNode.parent && rootNode.children.size() == 1){
            centerY = y + rootNode.getAllVisibleHeight()/2;
        }else {
            centerY = y + topNode.getAllVisibleHeight()/2;
        int centerY = y + topNode.getAllVisibleHeight()/2;
        //但是 有一种情况,就是顶部节点topNode没有配偶,如果topNode的祖先节点有配偶,且没有多子孙,此时topNode的高度比有配偶的祖先节点的高度小,所以此时的y是高度最高的祖先节点的y坐标
        if (topNode instanceof SimpleNode){
            Node tempNode = topNode.parent;
            while (tempNode.children.size() == 1){
                if (tempNode.height > topNode.height){
                    centerY = y + tempNode.getAllVisibleHeight()/2;
                    break;
                }
                tempNode = tempNode.parent;
            }
        }
        topNode.setViewPosition(x + topNode.getToSpecifyNodeWidth(rootNode) - topNode.width, centerY);
        // 根据topNode的位置,递归设置所有节点的位置
@@ -133,7 +138,7 @@
            for (Node child : parentNode.children) {
                if (child == node) {
                    centerYList.add(child.centerY);
                    currentBottom = currentBottom + node.getVisibleTopNode().top + node.getAllVisibleHeight();
                    currentBottom = currentBottom + child.getVisibleTop() + child.getAllVisibleHeight();
                    continue;
                }
                if (!child.isVisible){
@@ -148,7 +153,7 @@
            //取最大值、最小值 和 的中间
            int totalCenterY = 0;
            if (centerYList.size() > 0) {
            if (centerYList.size() > 1) {
                int min = centerYList.get(0);
                int max = centerYList.get(0);
                for (int i = 1; i < centerYList.size(); i++) {
@@ -161,6 +166,8 @@
                    }
                }
                totalCenterY = (min + max) / 2;
            }else{
                totalCenterY = centerYList.get(0);
            }
            if (parentNode.children.size() > 1) {
@@ -174,7 +181,21 @@
            node = parentNode;
        }
    }
    //获取 以当前节点为根节点的树 的最顶部的y坐标
    public int getVisibleTop(){
        if (!isVisible){
            return 0;
        }
        if (!isExpand){
            return top;
        }
        if (children.size() > 0){
            return Math.min(top,children.get(0).getVisibleTop());
        }
        return top;
    }
    //获取 以当前节点为根节点的树 的最深一节点
    public Node getVisibleTopNode(){
        Node topNode;
        if (!isVisible){
@@ -420,10 +441,12 @@
        }else {
            int heightSum = 0;
            for (Node child : node.children) {
                if (getNodeTreeHeightRecursion(child,considerVisible)!=0){
                int childHeight = getNodeTreeHeightRecursion(child,considerVisible);
                //如果有多个子节点且子节点有高度,那么子节点之间还有竖直间隔
                if (node.children.size() > 1 && childHeight > 0){
                    heightSum += verticalMargin;
                }
                heightSum += getNodeTreeHeightRecursion(child,considerVisible);
                heightSum += childHeight;
            }
            //子节点每两个之间还有竖直间隔
            return Math.max(node.height,heightSum);