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);
|
|
initView();
|
|
if (member.getIdentity() > 1 && member.getChildList() != null && member.getChildList().size() > 0){
|
//非 第一代 且 有子代, 节点宽度 = view宽度 + 前后路径长度 + 展开按钮宽度
|
width = pathLength + memberItemView.getMeasuredWidth() + pathLength + expandIconView.getMeasuredWidth();
|
}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();
|
}
|
|
private void initView() {
|
memberItemView = createItemView(member,true);
|
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);
|
}
|
|
@Override
|
public int getItemViewWidth() {
|
if (memberItemView != null){
|
return memberItemView.getMeasuredWidth();
|
}
|
return 0;
|
}
|
|
@Override
|
public int getItemViewHeight() {
|
if (memberItemView != null){
|
return memberItemView.getMeasuredHeight();
|
}
|
return 0;
|
}
|
}
|