Android 打造任意层级树形控件 考验你的数据结构和设计

2、Node


[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.tree.bean;

  2. import java.util.ArrayList;

  3. import java.util.List;

  4. import org.w3c.dom.NamedNodeMap;

  5. import android.util.Log;

  6. public class Node

  7. {

  8. private int id;

  9. /**

  10. * 根节点pId为0

  11. */

  12. private int pId = 0;

  13. private String name;

  14. /**

  15. * 当前的级别

  16. */

  17. private int level;

  18. /**

  19. * 是否展开

  20. */

  21. private boolean isExpand = false;

  22. private int icon;

  23. /**

  24. * 下一级的子Node

  25. */

  26. private List children = new ArrayList();

  27. /**

  28. * 父Node

  29. */

  30. private Node parent;

  31. public Node()

  32. {

  33. }

  34. public Node(int id, int pId, String name)

  35. {

  36. super();

  37. this.id = id;

  38. this.pId = pId;

  39. this.name = name;

  40. }

  41. public int getIcon()

  42. {

  43. return icon;

  44. }

  45. public void setIcon(int icon)

  46. {

  47. this.icon = icon;

  48. }

  49. public int getId()

  50. {

  51. return id;

  52. }

  53. public void setId(int id)

  54. {

  55. this.id = id;

  56. }

  57. public int getpId()

  58. {

  59. return pId;

  60. }

  61. public void setpId(int pId)

  62. {

  63. this.pId = pId;

  64. }

  65. public String getName()

  66. {

  67. return name;

  68. }

  69. public void setName(String name)

  70. {

  71. this.name = name;

  72. }

  73. public void setLevel(int level)

  74. {

  75. this.level = level;

  76. }

  77. public boolean isExpand()

  78. {

  79. return isExpand;

  80. }

  81. public List getChildren()

  82. {

  83. return children;

  84. }

  85. public void setChildren(List children)

  86. {

  87. this.children = children;

  88. }

  89. public Node getParent()

  90. {

  91. return parent;

  92. }

  93. public void setParent(Node parent)

  94. {

  95. this.parent = parent;

  96. }

  97. /**

  98. * 是否为跟节点

  99. *

  100. * @return

  101. */

  102. public boolean isRoot()

  103. {

  104. return parent == null;

  105. }

  106. /**

  107. * 判断父节点是否展开

  108. *

  109. * @return

  110. */

  111. public boolean isParentExpand()

  112. {

  113. if (parent == null)

  114. return false;

  115. return parent.isExpand();

  116. }

  117. /**

  118. * 是否是叶子界点

  119. *

  120. * @return

  121. */

  122. public boolean isLeaf()

  123. {

  124. return children.size() == 0;

  125. }

  126. /**

  127. * 获取level

  128. */

  129. public int getLevel()

  130. {

  131. return parent == null ? 0 : parent.getLevel() + 1;

  132. }

  133. /**

  134. * 设置展开

  135. *

  136. * @param isExpand

  137. */

  138. public void setExpand(boolean isExpand)

  139. {

  140. this.isExpand = isExpand;

  141. if (!isExpand)

  142. {

  143. for (Node node : children)

  144. {

  145. node.setExpand(isExpand);

  146. }

  147. }

  148. }

  149. }

包含了树节点一些常见的属性,一些常见的方法;对于getLevel,setExpand这些方法,大家可以好好看看~

有了Node,刚才的用法中,出现的就是我们Adapter所继承的超类:TreeListViewAdapter;核心代码都在里面,我们准备去一探究竟:

3、TreeListViewAdapter


代码不是很长,直接完整的贴出:

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. package com.zhy.tree.bean;

  2. import java.util.List;

  3. import android.content.Context;

  4. import android.view.LayoutInflater;

  5. import android.view.View;

  6. import android.view.ViewGroup;

  7. import android.widget.AdapterView;

  8. import android.widget.AdapterView.OnItemClickListener;

  9. import android.widget.BaseAdapter;

  10. import android.widget.ListView;

  11. public abstract class TreeListViewAdapter extends BaseAdapter

  12. {

  13. protected Context mContext;

  14. /**

  15. * 存储所有可见的Node

  16. */

  17. protected List mNodes;

  18. protected LayoutInflater mInflater;

  19. /**

  20. * 存储所有的Node

  21. */

  22. protected List mAllNodes;

  23. /**

  24. * 点击的回调接口

  25. */

  26. private OnTreeNodeClickListener onTreeNodeClickListener;

  27. public interface OnTreeNodeClickListener

  28. {

  29. void onClick(Node node, int position);

  30. }

  31. public void setOnTreeNodeClickListener(

  32. OnTreeNodeClickListener onTreeNodeClickListener)

  33. {

  34. this.onTreeNodeClickListener = onTreeNodeClickListener;

  35. }

  36. /**

  37. *

  38. * @param mTree

  39. * @param context

  40. * @param datas

  41. * @param defaultExpandLevel

  42. *            默认展开几级树

  43. * @throws IllegalArgumentException

  44. * @throws IllegalAccessException

  45. */

  46. public TreeListViewAdapter(ListView mTree, Context context, List datas,

  47. int defaultExpandLevel) throws IllegalArgumentException,

  48. IllegalAccessException

  49. {

  50. mContext = context;

  51. /**

  52. * 对所有的Node进行排序

  53. */

  54. mAllNodes = TreeHelper.getSortedNodes(datas, defaultExpandLevel);

  55. /**

  56. * 过滤出可见的Node

  57. */

  58. mNodes = TreeHelper.filterVisibleNode(mAllNodes);

  59. mInflater = LayoutInflater.from(context);

  60. /**

  61. * 设置节点点击时,可以展开以及关闭;并且将ItemClick事件继续往外公布

  62. */

  63. mTree.setOnItemClickListener(new OnItemClickListener()

  64. {

  65. @Override

  66. public void onItemClick(AdapterView<?> parent, View view,

  67. int position, long id)

  68. {

  69. expandOrCollapse(position);

  70. if (onTreeNodeClickListener != null)

  71. {

  72. onTreeNodeClickListener.onClick(mNodes.get(position),

  73. position);

  74. }

  75. }

  76. });

  77. }

  78. /**

  79. * 相应ListView的点击事件 展开或关闭某节点

  80. *

  81. * @param position

  82. */

  83. public void expandOrCollapse(int position)

  84. {

  85. Node n = mNodes.get(position);

  86. if (n != null)// 排除传入参数错误异常

  87. {

  88. if (!n.isLeaf())

  89. {

  90. n.setExpand(!n.isExpand());

  91. mNodes = TreeHelper.filterVisibleNode(mAllNodes);

  92. notifyDataSetChanged();// 刷新视图

  93. }

  94. }

  95. }

  96. @Override

  97. public int getCount()

  98. {

  99. return mNodes.size();

  100. }

  101. @Override

  102. public Object getItem(int position)

  103. {

  104. return mNodes.get(position);

  105. }

  106. @Override

  107. public long getItemId(int position)

  108. {

  109. return position;

  110. }

  111. @Override

  112. public View getView(int position, View convertView, ViewGroup parent)

  113. {

  114. Node node = mNodes.get(position);

  115. convertView = getConvertView(node, position, convertView, parent);

  116. // 设置内边距

  117. convertView.setPadding(node.getLevel() * 30, 3, 3, 3);

  118. return convertView;

  119. }

  120. public abstract View getConvertView(Node node, int position,

  121. View convertView, ViewGroup parent);

  122. }

首先我们的类继承自BaseAdapter,然后我们对应的数据集是,过滤出的可见的Node;

我们的构造方法默认接收4个参数:listview,context,mdatas,以及默认展开的级数:0只显示根节点;

可以在构造方法中看到:对用户传入的数据集做了排序,和过滤的操作;一会再看这些方法,这些方法我们使用了一个TreeHelper进行了封装。

注:如果你觉得你的Item布局十分复杂,且布局会展示Bean的其他数据,那么为了方便,你可以让Node中包含一个泛型T , 每个Node携带与之对于的Bean的所有数据;

可以看到我们还直接为Item设置了点击事件,因为我们树,默认就有点击父节点展开与关闭;但是为了让用户依然可用点击监听,我们自定义了一个点击的回调供用户使用;

当用户点击时,默认调用expandOrCollapse方法,将当然节点重置展开标志,然后重新过滤出可见的Node,最后notifyDataSetChanged即可;

其他的方法都是BaseAdapter默认的一些方法了。

下面我们看下TreeHelper中的一些方法:

4、TreeHelper


首先看TreeListViewAdapter构造方法中用到的两个方法:

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. /**

  2. * 传入我们的普通bean,转化为我们排序后的Node

  3. * @param datas

  4. * @param defaultExpandLevel

  5. * @return

  6. * @throws IllegalArgumentException

  7. * @throws IllegalAccessException

  8. */

  9. public static  List getSortedNodes(List datas,

  10. int defaultExpandLevel) throws IllegalArgumentException,

  11. IllegalAccessException

  12. {

  13. List result = new ArrayList();

  14. //将用户数据转化为List以及设置Node间关系

  15. List nodes = convetData2Node(datas);

  16. //拿到根节点

  17. List rootNodes = getRootNodes(nodes);

  18. //排序

  19. for (Node node : rootNodes)

  20. {

  21. addNode(result, node, defaultExpandLevel, 1);

  22. }

  23. return result;

  24. }

拿到用户传入的数据,转化为List以及设置Node间关系,然后根节点,从根往下遍历进行排序;

接下来看:filterVisibleNode

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. /**

  2. * 过滤出所有可见的Node

  3. *

  4. * @param nodes

  5. * @return

  6. */

  7. public static List filterVisibleNode(List nodes)

  8. {

  9. List result = new ArrayList();

  10. for (Node node : nodes)

  11. {

  12. // 如果为跟节点,或者上层目录为展开状态

  13. if (node.isRoot() || node.isParentExpand())

  14. {

  15. setNodeIcon(node);

  16. result.add(node);

  17. }

  18. }

  19. return result;

  20. }

过滤Node的代码很简单,遍历所有的Node,只要是根节点或者父节点是展开状态就添加返回;

最后看看这两个方法用到的别的一些私有方法:

[java]  view plain copy 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传 外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传

  1. /**

  2. * 将我们的数据转化为树的节点

  3. *

  4. * @param datas

  5. * @return

  6. * @throws NoSuchFieldException

  7. * @throws IllegalAccessException

  8. * @throws IllegalArgumentException

  9. */

  10. private static  List convetData2Node(List datas)

  11. throws IllegalArgumentException, IllegalAccessException

  12. {

  13. List nodes = new ArrayList();

  14. Node node = null;

  15. for (T t : datas)

  16. {

  17. int id = -1;

  18. int pId = -1;

  19. String label = null;

  20. Class<? extends Object> clazz = t.getClass();

  21. Field[] declaredFields = clazz.getDeclaredFields();

  22. for (Field f : declaredFields)

  23. {

  24. if (f.getAnnotation(TreeNodeId.class) != null)

  25. {

  26. f.setAccessible(true);

  27. id = f.getInt(t);

  28. }

  29. if (f.getAnnotation(TreeNodePid.class) != null)

  30. {

  31. f.setAccessible(true);

  32. pId = f.getInt(t);

  33. }

  34. if (f.getAnnotation(TreeNodeLabel.class) != null)

  35. {

  36. f.setAccessible(true);

  37. label = (String) f.get(t);

  38. }

  39. if (id != -1 && pId != -1 && label != null)

  40. {

  41. break;

  42. }

  43. }

  44. node = new Node(id, pId, label);

  45. nodes.add(node);

  46. }

  47. /**

  48. * 设置Node间,父子关系;让每两个节点都比较一次,即可设置其中的关系

  49. */

  50. for (int i = 0; i < nodes.size(); i++)

  51. {

  52. Node n = nodes.get(i);

  53. for (int j = i + 1; j < nodes.size(); j++)

  54. {

  55. Node m = nodes.get(j);

  56. if (m.getpId() == n.getId())

  57. {

  58. n.getChildren().add(m);

  59. m.setParent(n);

  60. } else if (m.getId() == n.getpId())

  61. {

  62. m.getChildren().add(n);

  63. n.setParent(m);

  64. }

  65. }

  66. }

  67. // 设置图片

  68. for (Node n : nodes)

  69. {

  70. setNodeIcon(n);

  71. }

  72. return nodes;

  73. }

  74. private static List getRootNodes(List nodes)

  75. {

  76. List root = new ArrayList();

  77. for (Node node : nodes)

  78. {

  79. if (node.isRoot())

  80. root.add(node);

  81. }

  82. return root;

  83. }

  84. /**

  85. * 把一个节点上的所有的内容都挂上去

  86. */

  87. private static void addNode(List nodes, Node node,

  88. int defaultExpandLeval, int currentLevel)

  89. {

  90. nodes.add(node);

  91. if (defaultExpandLeval >= currentLevel)

  92. {

  93. node.setExpand(true);

  94. }

  95. if (node.isLeaf())

  96. return;

  97. for (int i = 0; i < node.getChildren().size(); i++)

  98. {

  99. addNode(nodes, node.getChildren().get(i), defaultExpandLeval,

  100. currentLevel + 1);

  101. }

  102. }

  103. /**

  104. * 设置节点的图标

  105. *

  106. * @param node

  107. */

  108. private static void setNodeIcon(Node node)

  109. {

  110. if (node.getChildren().size() > 0 && node.isExpand())

  111. {

  112. node.setIcon(R.drawable.tree_ex);

  113. } else if (node.getChildren().size() > 0 && !node.isExpand())

  114. {

  115. node.setIcon(R.drawable.tree_ec);

  116. } else

  117. node.setIcon(-1);

  118. }

结尾

最后,针对上面谈的内容,给大家推荐一个Android资料,应该对大家有用。

首先是一个知识清单:(对于现在的Android及移动互联网来说,我们需要掌握的技术)

泛型原理丶反射原理丶Java虚拟机原理丶线程池原理丶
注解原理丶注解原理丶序列化
Activity知识体系(Activity的生命周期丶Activity的任务栈丶Activity的启动模式丶View源码丶Fragment内核相关丶service原理等)
代码框架结构优化(数据结构丶排序算法丶设计模式)
APP性能优化(用户体验优化丶适配丶代码调优)
热修复丶热升级丶Hook技术丶IOC架构设计
NDK(c编程丶C++丶JNI丶LINUX)
如何提高开发效率?
MVC丶MVP丶MVVM
微信小程序
Hybrid
Flutter

接下来是资料清单:(敲黑板!!!


1.数据结构和算法

2.设计模式

3.全套体系化高级架构视频;七大主流技术模块,视频+源码+笔记

4.面试专题资料包(怎么能少了一份全面的面试题总结呢~)

不论遇到什么困难,都不应该成为我们放弃的理由!共勉~

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0
(node.getChildren().size() > 0 && !node.isExpand())

  1. {

  2. node.setIcon(R.drawable.tree_ec);

  3. } else

  4. node.setIcon(-1);

  5. }

结尾

最后,针对上面谈的内容,给大家推荐一个Android资料,应该对大家有用。

首先是一个知识清单:(对于现在的Android及移动互联网来说,我们需要掌握的技术)

泛型原理丶反射原理丶Java虚拟机原理丶线程池原理丶
注解原理丶注解原理丶序列化
Activity知识体系(Activity的生命周期丶Activity的任务栈丶Activity的启动模式丶View源码丶Fragment内核相关丶service原理等)
代码框架结构优化(数据结构丶排序算法丶设计模式)
APP性能优化(用户体验优化丶适配丶代码调优)
热修复丶热升级丶Hook技术丶IOC架构设计
NDK(c编程丶C++丶JNI丶LINUX)
如何提高开发效率?
MVC丶MVP丶MVVM
微信小程序
Hybrid
Flutter

[外链图片转存中…(img-KL7ykSlR-1725658874061)]

接下来是资料清单:(敲黑板!!!


1.数据结构和算法

[外链图片转存中…(img-IABU0aps-1725658874061)]

2.设计模式

[外链图片转存中…(img-BTM56v6a-1725658874061)]

3.全套体系化高级架构视频;七大主流技术模块,视频+源码+笔记

[外链图片转存中…(img-5rh0QVSF-1725658874062)]

4.面试专题资料包(怎么能少了一份全面的面试题总结呢~)

[外链图片转存中…(img-HRxcvQes-1725658874062)]

不论遇到什么困难,都不应该成为我们放弃的理由!共勉~

如果你看到了这里,觉得文章写得不错就给个赞呗?如果你觉得那里值得改进的,请给我留言。一定会认真查询,修正不足。谢谢。

[外链图片转存中…(img-WkiN8qOe-1725658874062)]

加入社区》https://bbs.csdn.net/forums/4304bb5a486d4c3ab8389e65ecb71ac0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值