最近公司项目需要开发一个Android的树形部门结构,参考了网上的一些大佬写的,结合公司的需求写了一个,公司要求部门每个层级都可以选,且每一层级都只能取一个,然后是取消上级,下级全部取消,选中上级然后选下一级。话不多说上代码
1.首先是树结构数据的实体类
public class Node<T, B> {
/**
* 数据源的字段
*/
private String title;
private String bmid;
private String content;
private String code;
private String childBmid;
/**
* ****************** 分级树状等字段
*/
//传入的实体对象
public B bean;
//父级id子级pid
private T id;
//根节点pId为0
private T pId;
//节点名称
private String name;
//当前的级别
private int level;
//是否展开
private boolean isExpand = false;
//子节点数据 ,套用当前node
private List<Node> children = new ArrayList<>();
//父节点
private Node parent;
//是否被checked选中
private boolean isChecked;
public Node() {
}
public Node(T id, T pId, String name) {
super();
this.id = id;
this.pId = pId;
this.name = name;
}
public Node(T id, T pId, String name, B bean) {
super();
this.id = id;
this.pId = pId;
this.name = name;
this.bean = bean;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getBmid() {
return bmid;
}
public void setBmid(String bmid) {
this.bmid = bmid;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getChildBmid() {
return childBmid;
}
public void setChildBmid(String childBmid) {
this.childBmid = childBmid;
}
public T getId() {
return id;
}
public void setId(T id) {
this.id = id;
}
public T getpId() {
return pId;
}
public void setpId(T pId) {
this.pId = pId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setLevel(int level) {
this.level = level;
}
public boolean isExpand() {
return isExpand;
}
public List<Node> getChildren() {
return children;
}
public void setChildren(List<Node> children) {
this.children = children;
}
public Node getParent() {
return parent;
}
public void setParent(Node parent) {
this.parent = parent;
}
/**
* 是否为跟节点
*/
public boolean isRoot() {
return parent == null;
}
/**
* 判断父节点是否展开
*/
public boolean isParentExpand() {
if (parent == null)
return false;
return parent.isExpand();
}
/**
* 是否是叶子界点
*/
public boolean isLeaf() {
return children.size() == 0;
}
/**
* 获取当前所在层级,
* 设置层级间隔
*/
public int getLevel() {
return parent == null ? 0 : parent.getLevel() + 1;
}
/**
* 设置展开
*/
public void setExpand(boolean isExpand) {
this.isExpand = isExpand;
if (!isExpand) {
for (Node node : children) {
node.setExpand(isExpand);
}
}
}
}
2.树结构适配器
public class SimpleTreeAdapter extends TreeListViewAdapter {
private onNodeDataListener onNodeDataListener;
public List<Node> datas = new ArrayList<>();
public SimpleTreeAdapter(ListView mTree, Context context, List<Node> datas, int defaultExpandLevel) {
super(mTree, context, datas, defaultExpandLevel);
this.datas = datas;
}
@Override
public View getConvertView(final Node node, int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.item_tree_title, parent, false);
holder = new ViewHolder();
holder.tvName = (TextView) convertView.findViewById(R.id.tv_name);
holder.cb = (CheckBox) convertView.findViewById(R.id.check_box);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.cb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setChecked(node, holder.cb.isChecked());
//每一层级默认选中一个
traverseList(datas,node,holder.cb);
onNodeDataListener.onNodeDataListener(node);
}
});
if (node.isChecked()) {
holder.cb.setChecked(true);
holder.tvName.setTextColor(Color.parseColor("#1FCC7C"));
} else {
holder.cb.setChecked(false);
holder.tvName.setTextColor(Color.parseColor("#000000"));
}
holder.tvName.setText(node.getName());
return convertView;
}
class ViewHolder {
TextView tvName;
CheckBox cb;
}
public void setNodeDataListener(onNodeDataListener setNodeDataListener) {
this.onNodeDataListener = setNodeDataListener;
}
public interface onNodeDataListener {
void onNodeDataListener(Node node);
}
/**
* 遍历集合
* @param list
* @param node
* @param checkBox
*/
public void traverseList(List<Node> list,Node node,CheckBox checkBox) {
for (int i = 0; i < list.size(); i++) {
if (node.getLevel() == list.get(i).getLevel()) {
if (node.getId().equals(list.get(i).getId())) {
node.setChecked(checkBox.isChecked());
}else {
list.get(i).setChecked(false);
}
}else {
List<Node> children = list.get(i).getChildren();
traverseList(children,node,checkBox);
}
}
}
}
3.树结构节点关系
public class TreeHelper {
/**
* 传入node 返回排序后的Node
*/
public static List<Node> getSortedNodes(List<Node> datas,
int defaultExpandLevel) {
List<Node> result = new ArrayList<Node>();
// 设置Node间父子关系
List<Node> nodes = convetData2Node(datas);
// 拿到根节点
List<Node> rootNodes = getRootNodes(nodes);
// 排序以及设置Node间关系
for (Node node : rootNodes) {
addNode(result, node, defaultExpandLevel, 1);
}
return result;
}
/**
* 过滤出所有可见的Node
*/
public static List<Node> filterVisibleNode(List<Node> nodes) {
List<Node> result = new ArrayList<Node>();
for (Node node : nodes) {
// 如果为跟节点,或者上层目录为展开状态
if (node.isRoot() || node.isParentExpand()) {
result.add(node);
}
}
return result;
}
/**
* 设置Node间,父子关系;让每两个节点都比较一次,即可设置其中的关系
*/
private static List<Node> convetData2Node(List<Node> nodes) {
for (int i = 0; i < nodes.size(); i++) {
Node n = nodes.get(i);
for (int j = i + 1; j < nodes.size(); j++) {
Node m = nodes.get(j);
if (m.getpId() instanceof String) {
if (m.getpId().equals(n.getId())) {
n.getChildren().add(m);
m.setParent(n);
} else if (m.getId().equals(n.getpId())) {
m.getChildren().add(n);
n.setParent(m);
}
} else {
if (m.getpId() == n.getId()) {
n.getChildren().add(m);
m.setParent(n);
} else if (m.getId() == n.getpId()) {
m.getChildren().add(n);
n.setParent(m);
}
}
}
}
return nodes;
}
// 拿到根节点
private static List<Node> getRootNodes(List<Node> nodes) {
List<Node> root = new ArrayList<Node>();
for (Node node : nodes) {
if (node.isRoot())
root.add(node);
}
return root;
}
/**
* 把一个节点上的所有的内容都挂上去
*/
private static <T, B> void addNode(List<Node> nodes, Node<T, B> node,
int defaultExpandLeval, int currentLevel) {
nodes.add(node);
if (defaultExpandLeval >= currentLevel) {
node.setExpand(false);
}
if (node.isLeaf())
return;
for (int i = 0; i < node.getChildren().size(); i++) {
addNode(nodes, node.getChildren().get(i), defaultExpandLeval,
currentLevel + 1);
}
}
}
4.树结构适配器,这里做了一些小小的修改,之前大佬设计是可以全选,且上级选中,下级会全部选中,都有注释可以看看
public abstract class TreeListViewAdapter extends BaseAdapter {
private static final String TAG = "TreeListViewAdapter";
protected Context mContext;
/**
* 存储所有可见的Node
*/
protected List<Node> mNodes = new ArrayList<>();
protected LayoutInflater mInflater;
/**
* 存储所有的Node
*/
protected List<Node> mAllNodes = new ArrayList<>();
public TreeListViewAdapter(ListView mTree, Context context, List<Node> datas,
int defaultExpandLevel) {
mContext = context;
/**
* 对所有的Node进行排序
*/
mAllNodes = TreeHelper.getSortedNodes(datas, defaultExpandLevel);
/**
* 过滤出可见的Node
*/
mNodes = TreeHelper.filterVisibleNode(mAllNodes);
mInflater = LayoutInflater.from(context);
/**
* 设置节点点击时,可以展开以及关闭;并且将ItemClick事件继续往外公布
*/
mTree.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
expandOrCollapse(position);
}
});
}
/**
* 相应ListView的点击事件 展开或关闭某节点
*/
public void expandOrCollapse(int position) {
Node n = mNodes.get(position);
if (n != null) {// 排除传入参数错误异常
if (!n.isLeaf()) {
n.setExpand(!n.isExpand());
mNodes = TreeHelper.filterVisibleNode(mAllNodes);
notifyDataSetChanged();// 刷新视图
}
}
}
@Override
public int getCount() {
return mNodes.size();
}
@Override
public Object getItem(int position) {
return mNodes.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Node node = mNodes.get(position);
convertView = getConvertView(node, position, convertView, parent);
// 设置内边距
convertView.setPadding(node.getLevel() * 50, 3, 3, 3);
return convertView;
}
/**
* 设置多选
*/
protected void setChecked(final Node node, boolean checked) {
node.setChecked(checked);
setChildChecked(node, checked);
if (node.getParent() != null)
setNodeParentChecked(node.getParent(), checked);
notifyDataSetChanged();
}
/**
* 设置是否选中
*/
public <T, B> void setChildChecked(Node<T, B> node, boolean checked) {
Log.i(TAG, "isLeaf: "+node.isLeaf());
Log.i(TAG, "getName: "+node.getName());
Log.i(TAG, "getLevel: "+node.getLevel());
//是否是子节点
if (!node.isLeaf()) {
node.setChecked(checked);
//未选中就取消全部,选中不选中全部
if(!checked){
for (Node childrenNode : node.getChildren()) {
setChildChecked(childrenNode, checked);
}
}
} else {
node.setChecked(checked);
}
}
private void setNodeParentChecked(Node node, boolean checked) {
if (checked) {
node.setChecked(checked);
if (node.getParent() != null)
setNodeParentChecked(node.getParent(), checked);
} else {
List<Node> childrens = node.getChildren();
boolean isChecked = false;
for (Node children : childrens) {
if (children.isChecked()) {
isChecked = true;
}
}
//如果所有自节点都没有被选中 父节点也不选中
if (!isChecked) {
node.setChecked(checked);
}
if (node.getParent() != null)
setNodeParentChecked(node.getParent(), checked);
}
}
public abstract View getConvertView(Node node, int position,
View convertView, ViewGroup parent);
}
5.调用,这里是用到公司后台返回数据结构,解析装载数据到结构中
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private List<Node> mDatas = new ArrayList<>();//装载的总数据
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
for (int i = 0; i < 1; i++) {
Log.i(TAG, "i=====: "+i);
Node root = new Node();
root.setId("1111");
root.setpId(0);
root.setLevel(1);
root.setName("一级部门-"+i);
mDatas.add(root);
for (int j = 0; j < 2; j++) {
Log.i(TAG, "j=====: "+j);
Node secondNode = new Node();
secondNode.setId("2222"+j);
secondNode.setpId("1111");
secondNode.setLevel(2);
secondNode.setName("二级部门-"+j);
mDatas.add(secondNode);
for (int k = 0; k < 3; k++) {
Log.i(TAG, "k=====: "+k);
Node ThreeNode = new Node();
ThreeNode.setId("3333"+k);
ThreeNode.setpId("2222"+j);
ThreeNode.setLevel(3);
ThreeNode.setName("三级部门-"+k);
mDatas.add(ThreeNode);
}
}
}
Log.i(TAG, "onCreate: "+mDatas.size());
ListView listView = findViewById(R.id.listview);
SimpleTreeAdapter mAdapter = new SimpleTreeAdapter(listView, this, mDatas, 10);
listView.setAdapter(mAdapter);
mAdapter.setNodeDataListener(new SimpleTreeAdapter.onNodeDataListener() {
@Override
public void onNodeDataListener(Node node) {
// 这块文字没注释,没注释看的清楚 0.0
// node返回的是当前的节点数据的值
// 因为所有的节点数据都已经挂在node上了,
// 所有在node中可以看到当前节点的parent值,以及子节点值,为null代表没有
//
// 结构我说清楚了,具体的这个check怎么处理,看你们需求,我就不多说了。
Log.i(TAG, "onNodeDataListener: "+node.getChildren());
}
});
}
}
以上就树形结构的开发,每个公司的业务不一样,可以根据不同需求修改,参考连接借鉴的链接