• Android打造一个高性能无限层级显示的树形控件(Android树形控件)


    Android使用ListView实现一个高性能无限层级显示的树形控件:

    最近公司的Android项目里有一个地方需要选择某公司的所有部门,因为手机屏幕有限所以并不能像网页那样显示树状结构,但是如果只是用列表依次显示所有的部门又会让用户很难找到想要找的部门(即使加上搜索功能也很难表现出层级关系),由于系统控件ExpandableListView 只能显示两级,加上数据集的组织比较麻烦,所以就使用ListView来实现如下的树形展示效果。至于为什么使用listview最大的好处就是它自带控件复用功能,我们不用去处理这个复杂的问题。最终效果如下:

    Center

    分析:

    因为要展示的是一个树形结构,所以每一条记录必须拥有一个指向父亲节点的字段。为了体现出层级结构,其实就是增加缩进就可以了。当然说起来很简单,但是做起来的时候会有很多地方值得注意,比如说如何处理展开和收缩,以及跨级展开,收缩,如何得到当前是第几层从而处理缩进等等,再比如说如果数据量比较大的话性能怎么样等等一系列问题。

    接下来我们就以层级显示一个公司的所有部门为需求来实现一下,其实只要具有树形结构我们都可以这样做。

    实现思路以及使用方法:

    首先我们要定义一个抽象类,其中包含必需的字段和方法:

     
    
    1. /**
    2. * Created by HQOCSHheqing on 2016/8/2.
    3. *
    4. * @description 节点抽象类(泛型T主要是考虑到ID和parentID有可能是int型也有可能是String型
    5. * 即这里可以传入Integer或者String,具体什么类型由子类指定
    6. ,因为这两种类型比较是否相等的方式不同:一个是用 “==”,一个是用 equals() 函数)
    7. */
    8. public abstract class Node {
    9. private int _level = -1;//当前节点的层级,初始值-1 后面会讲到
    10. private List _childrenList = new ArrayList<>();//所有的孩子节点
    11. private Node _parent;//父亲节点
    12. private int _icon;//图标资源ID
    13. private boolean isExpand = false;//当前状态是否展开
    14. public abstract T get_id();//得到当前节点ID
    15. public abstract T get_parentId();//得到当前节点的父ID
    16. public abstract String get_label();//要显示的内容
    17. public abstract boolean parent(Node dest);//判断当前节点是否是dest的父亲节点
    18. public abstract boolean child(Node dest);//判断当前节点是否是dest的孩子节点
    19. public int get_level() {
    20. if (_level == -1){//如果是 -1 的话就递归获取
    21. //因为是树形结构,所以此处想要得到当前节点的层级
    22. //,必须递归调用,但是递归效率低下,如果每次都递归获取会严重影响性能,所以我们把第一次
    23. //得到的结果保存起来避免每次递归获取
    24. int level = _parent == null ? 1 : _parent.get_level()+1;
    25. _level = level;
    26. return _level;
    27. }
    28. return _level;
    29. }
    30. public void set_level(int _level) {
    31. this._level = _level;
    32. }
    33. public List get_childrenList() {
    34. return _childrenList;
    35. }
    36. public void set_childrenList(List _childrenList) {
    37. this._childrenList = _childrenList;
    38. }
    39. public Node get_parent() {
    40. return _parent;
    41. }
    42. public void set_parent(Node _parent) {
    43. this._parent = _parent;
    44. }
    45. public int get_icon() {
    46. return _icon;
    47. }
    48. public void set_icon(int _icon) {
    49. this._icon = _icon;
    50. }
    51. public boolean isExpand() {
    52. return isExpand;
    53. }
    54. public void setIsExpand(boolean isExpand) {
    55. this.isExpand = isExpand;
    56. if (isExpand){
    57. _icon = R.mipmap.collapse;
    58. }else{
    59. _icon = R.mipmap.expand;
    60. }
    61. }
    62. public boolean isRoot(){
    63. return _parent == null;
    64. }
    65. public boolean isLeaf(){
    66. return _childrenList.size() <= 0;
    67. }
    68. }

    将我们要树状显示的实体继承此抽象类,我们一部门ID以及parentid为integer型为例:

     
    
    1. /**
    2. * Created by HQOCSHheqing on 2016/8/2.
    3. *
    4. * @description 部门类(继承Node),此处的泛型Integer是因为ID和parentID都为int
    5. * ,如果为String传入泛型String即可,如果传入String,记得修改parent和child方法,因为比较相等的方式不同。
    6. */
    7. public class Dept extends Node{
    8. private int id;//部门ID
    9. private int parentId;//父亲节点ID
    10. private String name;//部门名称
    11. public Dept() {
    12. }
    13. public Dept(int id, int parentId, String name) {
    14. this.id = id;
    15. this.parentId = parentId;
    16. this.name = name;
    17. }
    18. /**
    19. * 此处返回节点ID
    20. * @return
    21. */
    22. @Override
    23. public Integer get_id() {
    24. return id;
    25. }
    26. /**
    27. * 此处返回父亲节点ID
    28. * @return
    29. */
    30. @Override
    31. public Integer get_parentId() {
    32. return parentId;
    33. }
    34. @Override
    35. public String get_label() {
    36. return name;
    37. }
    38. @Override
    39. public boolean parent(Node dest) {
    40. if (id == ((Integer)dest.get_parentId()).intValue()){
    41. return true;
    42. }
    43. return false;
    44. }
    45. @Override
    46. public boolean child(Node dest) {
    47. if (parentId == ((Integer)dest.get_id()).intValue()){
    48. return true;
    49. }
    50. return false;
    51. }
    52. public int getId() {
    53. return id;
    54. }
    55. public void setId(int id) {
    56. this.id = id;
    57. }
    58. public int getParentId() {
    59. return parentId;
    60. }
    61. public void setParentId(int parentId) {
    62. this.parentId = parentId;
    63. }
    64. public String getName() {
    65. return name;
    66. }
    67. public void setName(String name) {
    68. this.name = name;
    69. }
    70. }

    写一个帮助类:主要是整理节点与节点之间的关系,构造森林(有可能构造出多个树)。

     
    
    1. /**
    2. * Created by HQOCSHheqing on 2016/8/2.
    3. *
    4. * @description 节点帮助类
    5. */
    6. public class NodeHelper {
    7. /**
    8. * 传入所有的要展示的节点数据
    9. * @param nodeList 返回值是所有的根节点
    10. * @return
    11. */
    12. public static List sortNodes(List nodeList){
    13. List rootNodes = new ArrayList<>();
    14. int size = nodeList.size();
    15. Node m;
    16. Node n;
    17. //两个for循环整理出所有数据之间的斧子关系,最后会构造出一个森林(就是可能有多棵树)
    18. for (int i = 0;i < size;i++){
    19. m = nodeList.get(i);
    20. for (int j = i+1;j < size;j++){
    21. n = nodeList.get(j);
    22. if (m.parent(n)){
    23. m.get_childrenList().add(n);
    24. n.set_parent(m);
    25. }else if (m.child(n)){
    26. n.get_childrenList().add(m);
    27. m.set_parent(n);
    28. }
    29. }
    30. }
    31. //找出所有的树根,同事设置相应的图标(后面你会发现其实这里的主要
    32. // 作用是设置叶节点和非叶节点的图标)
    33. for (int i = 0;i < size;i++){
    34. m = nodeList.get(i);
    35. if (m.isRoot()){
    36. rootNodes.add(m);
    37. }
    38. setNodeIcon(m);
    39. }
    40. nodeList.clear();//此时所有的关系已经整理完成了
    41. // ,森林构造完成,可以清空之前的数据,释放内存,提高性能
    42. // ,如果之前的数据还有用的话就不清空
    43. nodeList = rootNodes;//返回所有的根节点
    44. rootNodes = null;
    45. return nodeList;
    46. }
    47. /**
    48. * 设置图标
    49. * @param node
    50. */
    51. private static void setNodeIcon(Node node){
    52. if (!node.isLeaf()){
    53. if (node.isExpand()){
    54. node.set_icon(R.mipmap.collapse);
    55. }else{
    56. node.set_icon(R.mipmap.expand);
    57. }
    58. }else{
    59. node.set_icon(-1);
    60. }
    61. }
    62. }

    为ListView构造出一个适配器:这个适配器就是经常用到的适配器的写法:

     
    
    1. /**
    2. * Created by HQOCSHheqing on 2016/8/3.
    3. *
    4. * @description 适配器类,就是listview最常见的适配器写法
    5. */
    6. public class NodeTreeAdapter extends BaseAdapter{
    7. //大家经常用ArrayList,但是这里为什么要使用LinkedList
    8. // ,后面大家会发现因为这个list会随着用户展开、收缩某一项而频繁的进行增加、删除元素操作,
    9. // 因为ArrayList是数组实现的,频繁的增删性能低下,而LinkedList是链表实现的,对于频繁的增删
    10. //操作性能要比ArrayList好。
    11. private LinkedList nodeLinkedList;
    12. private LayoutInflater inflater;
    13. private int retract;//缩进值
    14. private Context context;
    15. public NodeTreeAdapter(Context context,ListView listView,LinkedList linkedList){
    16. inflater = LayoutInflater.from(context);
    17. this.context = context;
    18. nodeLinkedList = linkedList;
    19. listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    20. @Override
    21. public void onItemClick(AdapterView parent, View view, int position, long id) {
    22. expandOrCollapse(position);
    23. }
    24. });
    25. //缩进值,大家可以将它配置在资源文件中,从而实现适配
    26. retract = (int)(context.getResources().getDisplayMetrics().density*10+0.5f);
    27. }
    28. /**
    29. * 展开或收缩用户点击的条目
    30. * @param position
    31. */
    32. private void expandOrCollapse(int position){
    33. Node node = nodeLinkedList.get(position);
    34. if (node != null && !node.isLeaf()){
    35. boolean old = node.isExpand();
    36. if (old){
    37. List nodeList = node.get_childrenList();
    38. int size = nodeList.size();
    39. Node tmp = null;
    40. for (int i = 0;i < size;i++){
    41. tmp = nodeList.get(i);
    42. if (tmp.isExpand()){
    43. collapse(tmp,position+1);
    44. }
    45. nodeLinkedList.remove(position+1);
    46. }
    47. }else{
    48. nodeLinkedList.addAll(position + 1, node.get_childrenList());
    49. }
    50. node.setIsExpand(!old);
    51. notifyDataSetChanged();
    52. }
    53. }
    54. /**
    55. * 递归收缩用户点击的条目
    56. * 因为此中实现思路是:当用户展开某一条时,就将该条对应的所有子节点加入到nodeLinkedList
    57. * ,同时控制缩进,当用户收缩某一条时,就将该条所对应的子节点全部删除,而当用户跨级缩进时
    58. * ,就需要递归缩进其所有的孩子节点,这样才能保持整个nodeLinkedList的正确性,同时这种实
    59. * 现方式避免了每次对所有数据进行处理然后插入到一个list,最后显示出来,当数据量一大,就会卡顿,
    60. * 所以这种只改变局部数据的方式性能大大提高。
    61. * @param position
    62. */
    63. private void collapse(Node node,int position){
    64. node.setIsExpand(false);
    65. List nodes = node.get_childrenList();
    66. int size = nodes.size();
    67. Node tmp = null;
    68. for (int i = 0;i < size;i++){
    69. tmp = nodes.get(i);
    70. if (tmp.isExpand()){
    71. collapse(tmp,position+1);
    72. }
    73. nodeLinkedList.remove(position+1);
    74. }
    75. }
    76. @Override
    77. public int getCount() {
    78. return nodeLinkedList.size();
    79. }
    80. @Override
    81. public Object getItem(int position) {
    82. return nodeLinkedList.get(position);
    83. }
    84. @Override
    85. public long getItemId(int position) {
    86. return position;
    87. }
    88. @Override
    89. public View getView(int position, View convertView, ViewGroup parent) {
    90. final ViewHolder holder;
    91. if (convertView == null){
    92. convertView = inflater.inflate(R.layout.tree_listview_item,null);
    93. holder = new ViewHolder();
    94. holder.imageView = (ImageView)convertView.findViewById(R.id.id_treenode_icon);
    95. holder.label = (TextView)convertView.findViewById(R.id.id_treenode_label);
    96. holder.confirm = (LinearLayout)convertView.findViewById(R.id.id_confirm);
    97. convertView.setTag(holder);
    98. }else{
    99. holder = (ViewHolder)convertView.getTag();
    100. }
    101. Node node = nodeLinkedList.get(position);
    102. holder.label.setText(node.get_label());
    103. if(node.get_icon() == -1){
    104. holder.imageView.setVisibility(View.INVISIBLE);
    105. }else{
    106. holder.imageView.setVisibility(View.VISIBLE);
    107. holder.imageView.setImageResource(node.get_icon());
    108. }
    109. holder.confirm.setTag(position);
    110. holder.confirm.setOnClickListener(new View.OnClickListener() {
    111. @Override
    112. public void onClick(View v) {
    113. Toast.makeText(context,"选中:"+v.getTag(),Toast.LENGTH_SHORT).show();
    114. }
    115. });
    116. convertView.setPadding(node.get_level()*retract,5,5,5);//处理缩进
    117. return convertView;
    118. }
    119. static class ViewHolder{
    120. public ImageView imageView;
    121. public TextView label;
    122. public LinearLayout confirm;
    123. }
    124. }

    所有重点都在注释中说明,此处省略。

    最后我们来应用一下:

    首先创建一个activity:

     
    
    1. /**
    2. * Created by HQOCSHheqing on 2016/8/4.
    3. *
    4. * @description
    5. */
    6. public class TreeTestActivity extends Activity{
    7. private ListView mListView;
    8. private NodeTreeAdapter mAdapter;
    9. private LinkedList mLinkedList = new LinkedList<>();
    10. @Override
    11. protected void onCreate(Bundle savedInstanceState) {
    12. super.onCreate(savedInstanceState);
    13. setContentView(R.layout.select_dept_layout);
    14. mListView = (ListView)findViewById(R.id.id_tree);
    15. mAdapter = new NodeTreeAdapter(this,mListView,mLinkedList);
    16. mListView.setAdapter(mAdapter);
    17. initData();
    18. }
    19. private void initData(){
    20. List data = new ArrayList<>();
    21. addOne(data);
    22. mLinkedList.addAll(NodeHelper.sortNodes(data));
    23. mAdapter.notifyDataSetChanged();
    24. }
    25. private void addOne(List data){
    26. data.add(new Dept(1, 0, "总公司"));//可以直接注释掉此项,即可构造一个森林
    27. data.add(new Dept(2, 1, "一级部一级部门一级部门一级部门门级部门一级部门级部门一级部门一级部门门级部一级"));
    28. data.add(new Dept(3, 1, "一级部门"));
    29. data.add(new Dept(4, 1, "一级部门"));
    30. data.add(new Dept(222, 5, "二级部门--测试1"));
    31. data.add(new Dept(223, 5, "二级部门--测试2"));
    32. data.add(new Dept(5, 1, "一级部门"));
    33. data.add(new Dept(224, 5, "二级部门--测试3"));
    34. data.add(new Dept(225, 5, "二级部门--测试4"));
    35. data.add(new Dept(6, 1, "一级部门"));
    36. data.add(new Dept(7, 1, "一级部门"));
    37. data.add(new Dept(8, 1, "一级部门"));
    38. data.add(new Dept(9, 1, "一级部门"));
    39. data.add(new Dept(10, 1, "一级部门"));
    40. for (int i = 2;i <= 10;i++){
    41. for (int j = 0;j < 10;j++){
    42. data.add(new Dept(1+(i - 1)*10+j,i, "二级部门"+j));
    43. }
    44. }
    45. for (int i = 0;i < 5;i++){
    46. data.add(new Dept(101+i,11, "三级部门"+i));
    47. }
    48. for (int i = 0;i < 5;i++){
    49. data.add(new Dept(106+i,22, "三级部门"+i));
    50. }
    51. for (int i = 0;i < 5;i++){
    52. data.add(new Dept(111+i,33, "三级部门"+i));
    53. }
    54. for (int i = 0;i < 5;i++){
    55. data.add(new Dept(115+i,44, "三级部门"+i));
    56. }
    57. for (int i = 0;i < 5;i++){
    58. data.add(new Dept(401+i,101, "四级部门"+i));
    59. }
    60. }
    61. }

    布局文件:

    #select_dept_layout.xml

     
    
    1. android:layout_width="match_parent"
    2. android:layout_height="match_parent">
    3. android:id="@+id/id_tree"
    4. android:layout_width="fill_parent"
    5. android:layout_height="fill_parent"
    6. android:divider="#aaa"
    7. android:dividerHeight="1px"/>

    #tree_listview_item.xml

     
    
    1. android:layout_width="match_parent"
    2. android:layout_height="wrap_content">
    3. android:id="@+id/id_treenode_icon"
    4. android:layout_width="wrap_content"
    5. android:layout_height="wrap_content"
    6. android:layout_centerVertical="true"
    7. android:layout_marginLeft="5dp"
    8. android:layout_marginRight="5dp"
    9. android:src="@mipmap/expand" />
    10. android:id="@+id/id_confirm"
    11. android:layout_width="wrap_content"
    12. android:layout_height="match_parent"
    13. android:layout_alignParentRight="true"
    14. android:layout_centerVertical="true"
    15. android:clickable="true"
    16. android:paddingBottom="8dp"
    17. android:paddingLeft="15dp"
    18. android:paddingRight="15dp"
    19. android:paddingTop="8dp">
    20. android:layout_width="25dp"
    21. android:layout_height="25dp"
    22. android:background="@drawable/login_checkbox_selector"
    23. android:scaleType="centerInside" />
    24. android:id="@+id/id_treenode_label"
    25. android:layout_width="wrap_content"
    26. android:layout_height="wrap_content"
    27. android:layout_centerVertical="true"
    28. android:layout_toLeftOf="@id/id_confirm"
    29. android:layout_toRightOf="@id/id_treenode_icon"
    30. android:textColor="@android:color/black"
    31. android:textSize="12sp" />

    实现思路:

     
    
    1. 要实现一个层级显示的控件,但是又不确定有几层,而内置的ExpandableListView又只能显示两级关系,所以只能自己改造已有的控件或者完全自己写,但是再仔细考虑会发现这两种方式都不太好实现,都需要我们自己处理层级,而且完全自己写还有一个致命问题就是复用问题,即使实现了也会有很多bug,想想会让我们无从下手。但是我们换一种思路,要想展示层级关系,不就是父级与子级之间的缩进不同吗,所以我们是否可以通过控制ListView的各个条目的缩进从而达到这个目的,这样我们就不用管复杂的复用问题了,但是怎么才能实现一层一层的展开、收缩呢?不要想的太复杂,换一种思路,其实就是更改ListView的数据集,展开时将要展开的数据放入到数据集中,收缩时从数据集中删除相应数据即可,这样我们每次只处理与用户点击有关的局部数据,而不用每次遍历所有的数据进行筛选,这样性能会大大提高。
    2. 在实现这个功能之前也上网找了资料,其中深受这篇博客(http://blog.csdn.net/lmj623565791/article/details/40212367)的启发,是站在巨人的肩膀上,在此感谢。
    3. 完整源代码:[http://download.csdn.net/detail/hqocshheqing/9601185][http_download.csdn.net_detail_hqocshheqing_9601185]
    4. github地址:[https://github.com/heqinghqocsh/TreeView][https_github.com_heqinghqocsh_TreeView]
  • 相关阅读:
    有电闭锁继电器 YDB-100 100V 辅助电源DC110V JOSEF约瑟 板后安装
    C++——vector(3)
    自动化信息收集工具 水泽 使用教程
    双功能交联剂丨Lumiprobe 磺基花青7二羧酸研究
    【Linux】系统进程的概念
    移动端页面秒开优化总结
    Linux多线程
    C++:类与对象(2)
    pytorch深度学习实战lesson13
    标签名选择器、id选择器、class类型选择器、组合选择器
  • 原文地址:https://blog.csdn.net/qq_27248989/article/details/134238404