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

  1. private int icon;

  2. /**

  3. * 下一级的子Node

  4. */

  5. private List children = new ArrayList();

  6. /**

  7. * 父Node

  8. */

  9. private Node parent;

  10. public Node()

  11. {

  12. }

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

  14. {

  15. super();

  16. this.id = id;

  17. this.pId = pId;

  18. this.name = name;

  19. }

  20. public int getIcon()

  21. {

  22. return icon;

  23. }

  24. public void setIcon(int icon)

  25. {

  26. this.icon = icon;

  27. }

  28. public int getId()

  29. {

  30. return id;

  31. }

  32. public void setId(int id)

  33. {

  34. this.id = id;

  35. }

  36. public int getpId()

  37. {

  38. return pId;

  39. }

  40. public void setpId(int pId)

  41. {

  42. this.pId = pId;

  43. }

  44. public String getName()

  45. {

  46. return name;

  47. }

  48. public void setName(String name)

  49. {

  50. this.name = name;

  51. }

  52. public void setLevel(int level)

  53. {

  54. this.level = level;

  55. }

  56. public boolean isExpand()

  57. {

  58. return isExpand;

  59. }

  60. public List getChildren()

  61. {

  62. return children;

  63. }

  64. public void setChildren(List children)

  65. {

  66. this.children = children;

  67. }

  68. public Node getParent()

  69. {

  70. return parent;

  71. }

  72. public void setParent(Node parent)

  73. {

  74. this.parent = parent;

  75. }

  76. /**

  77. * 是否为跟节点

  78. *

  79. * @return

  80. */

  81. public boolean isRoot()

  82. {

  83. return parent == null;

  84. }

  85. /**

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

  87. *

  88. * @return

  89. */

  90. public boolean isParentExpand()

  91. {

  92. if (parent == null)

  93. return false;

  94. return parent.isExpand();

  95. }

  96. /**

  97. * 是否是叶子界点

  98. *

  99. * @return

  100. */

  101. public boolean isLeaf()

  102. {

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

  104. }

  105. /**

  106. * 获取level

  107. */

  108. public int getLevel()

  109. {

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

  111. }

  112. /**

  113. * 设置展开

  114. *

  115. * @param isExpand

  116. */

  117. public void setExpand(boolean isExpand)

  118. {

  119. this.isExpand = isExpand;

  120. if (!isExpand)

  121. {

  122. for (Node node : children)

  123. {

  124. node.setExpand(isExpand);

  125. }

  126. }

  127. }

  128. }

包含了树节点一些常见的属性,一些常见的方法;对于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)

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
img
img
img
img

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
img

最后我还整理了很多Android中高级的PDF技术文档。以及一些大厂面试真题解析文档。需要的朋友都可以点击GitHub直接获取方式

image

Android高级架构师之路很漫长,一起共勉吧!

{

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

  2. n.setParent(m);

  3. }

  4. }

  5. }

  6. // 设置图片

  7. for (Node n : nodes)

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-SHBgkaIE-1711043890923)]
[外链图片转存中…(img-guNNfH9b-1711043890924)]
[外链图片转存中…(img-gYYUWQx4-1711043890924)]
[外链图片转存中…(img-Jd9b3NFG-1711043890925)]

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-4gzeH7vF-1711043890926)]

最后我还整理了很多Android中高级的PDF技术文档。以及一些大厂面试真题解析文档。需要的朋友都可以点击GitHub直接获取方式

[外链图片转存中…(img-vXtxK05C-1711043890927)]

Android高级架构师之路很漫长,一起共勉吧!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值