Linjiajia
2023-09-12 efafbbf142c81c233c71de636a2d3ce9dc2124f0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
package com.application.zhangshi_app_android.widget;
 
import android.content.Context;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.View;
import android.widget.ImageView;
 
import com.application.zhangshi_app_android.R;
import com.application.zhangshi_app_android.bean.HomeRootBean;
 
import java.util.List;
 
/**
 * @author Ljj
 * @date 2023.08.13. 7:35
 * @desc 单亲节点
 */
public class SimpleNode extends Node{
    private View memberItemView;//成员view
    private ImageView expandIconView;//展开按钮
 
 
    public SimpleNode(Context context,HomeRootBean member) {
        super(context,member);
        if (member.getIdentity() > 1 && member.getChildList() != null && member.getChildList().size() > 0){
            //非 第一代 且 有子代, 节点宽度 = view宽度 + 前后路径长度 + 展开按钮宽度
            width = pathLength + getItemViewWidth() + pathLength  + getExpandIconWidth();
        }else if (member.getIdentity() > 1 && (member.getChildList() == null || member.getChildList().size() == 0)) {
            //非 第一代 且 无子代, 节点宽度 = view宽度 + 前路径长度
            width = getItemViewWidth() + pathLength;
        }else if (member.getIdentity() == 1 && member.getChildList() != null && member.getChildList().size() > 0) {
            //第一代 且 有子代, 节点宽度 = view宽度 + 后路径长度 + 展开按钮宽度
            width = getItemViewWidth() + pathLength + getExpandIconWidth();
        }else if (member.getIdentity() == 1 && (member.getChildList() == null || member.getChildList().size() == 0)) {
            //第一代 且 无子代, 节点宽度 = view宽度
            width = getItemViewWidth();
        }
        //单亲节点 高度 = view高度
        height = getItemViewHeight();
        initView();
    }
 
    private void initView() {
        memberItemView = createItemView(member);
        expandIconView = createExpandIconView();
        viewList.add(memberItemView);
        viewList.add(expandIconView);
 
        if (isExpand){
            expandIconView.setBackgroundResource(R.drawable.ic_collapse);
        }else {
            expandIconView.setBackgroundResource(R.drawable.ic_expand);
        }
        expandIconView.setOnClickListener(v -> {
            if (isExpand){
                expandIconView.setBackgroundResource(R.drawable.ic_expand);
                for (Node child : children) {
                    child.setVisible(false);
                }
                isExpand = false;
            }else {
                expandIconView.setBackgroundResource(R.drawable.ic_collapse);
                for (Node child : children) {
                    child.setVisible(true);
                }
                isExpand = true;
            }
        });
    }
 
    @Override
    protected void setViewPosition(int x, int centerY) {
        left = x;
        right = left + width;
        this.centerY = centerY;
        top = centerY - height/2;
        bottom = centerY + height/2;
 
        if (member.getIdentity() > 1){//非 第一代 才有前置连线
            viewLeft = left + pathLength;
        }else {
            viewLeft = left;
        }
        memberItemView.setX(viewLeft);
        memberItemView.setY(centerY - memberItemView.getMeasuredHeight()/2f);
        viewRight = viewLeft + memberItemView.getMeasuredWidth();
        viewTop = (int) memberItemView.getY();
        viewBottom = (int) (memberItemView.getY() + memberItemView.getMeasuredHeight());
 
        expandIconView.setX(viewRight + pathLength);
        expandIconView.setY(centerY - expandIconView.getMeasuredHeight()/2f);
 
        right = (int) (expandIconView.getMeasuredWidth() + expandIconView.getX());
 
        setPath();
    }
 
    private void setPath() {
        prePathList.clear();
        nextPathList.clear();
 
        if (member.getChildList() != null && member.getChildList().size() > 0){//有子节点,才有后置连线,才有展开按钮
            expandIconView.setVisibility(View.VISIBLE);
            Path path = new Path();
            path.moveTo(viewRight, centerY);
            path.lineTo(expandIconView.getX(), centerY);
            nextPathList.add(path);
        }else {
            expandIconView.setVisibility(View.GONE);
            viewList.remove(expandIconView);
            right = viewRight;
        }
        if (member.getIdentity() > 1){//非 第一代 才有前置连线
            Path path = new Path();
            path.moveTo(viewLeft, centerY);
            path.lineTo(left, centerY);
            prePathList.add(path);
        }
    }
 
    //设置分叉点Y坐标集合
    public void setForkYList(List<Integer> yList){
        if (yList == null || yList.size() < 2){
            return;
        }
        forkPathList.clear();
 
        //取出最大和最小
        int rBottom = 0;
        int rTop = 0;
        for (int i = 0; i < yList.size(); i++) {
            if (i == 0){
                rTop = yList.get(i);
                rBottom = yList.get(i);
            }else {
                if (yList.get(i) > rBottom){
                    rBottom = yList.get(i);
                }
                if (yList.get(i) < rTop){
                    rTop = yList.get(i);
                }
            }
        }
        yList.remove((Integer) rTop);
        yList.remove((Integer) rBottom);
 
        float rLeft = right + pathLength;
        float rRight = rLeft + pathLength;
        Path path = new Path();
        //右上角
        path.moveTo(rRight, rTop);
        // ---
        path.lineTo(rLeft + cornerRadius, rTop);
        // 添加圆弧路径,左上角的圆角
        path.arcTo(new RectF(rLeft, rTop, rLeft+2*cornerRadius, rTop+2*cornerRadius), 270, -90, false);
        // ----
        // |
        // |
        path.lineTo(rLeft, rBottom - cornerRadius);
        // 添加圆弧路径,左下角的圆角
        path.arcTo(new RectF(rLeft, rBottom - 2 * cornerRadius, rLeft + 2 * cornerRadius, rBottom), 180, -90, false);
        // ----
        // |
        // |
        // ----
        path.lineTo(rRight, rBottom);
        //移到左中间
        path.moveTo(rLeft, centerY);
        //    ----
        // ___|
        //    |
        //    ----
        path.lineTo(rLeft - pathLength, centerY);
        for (Integer y : yList) {
            path.moveTo(rLeft, y);
            path.lineTo(rRight, y);
        }
        forkPathList.add(path);
    }
}