美文网首页
Android 树形结构(AndroidTreeView)

Android 树形结构(AndroidTreeView)

作者: I_Gisvity | 来源:发表于2017-11-23 14:46 被阅读0次

    膜拜大神:https://github.com/bmelnychuk/AndroidTreeView

    image.png

    Features

      1. N - level expandable/collapsable tree
      1. Custom values, views, styles for nodes
      1. Save state after rotation
      1. Selection mode for nodes
      1. Dynamic add/remove node

    Known Limitations

    • For Android 4.0 (+/- nearest version) if you have too deep view hierarchy and with tree its easily possible, your app may crash



    [图片上传失败...(image-b1b41d-1511419576958)]
    [图片上传失败...(image-66a957-1511419576958)]


    image

    Integration

    1) Add library as a dependency to your project

    compile 'com.github.bmelnychuk:atv:1.2.+'

    2) Create your tree starting from root element. TreeNode.root() element will not be displayed so it doesn't require anything to be set.

    TreeNode root = TreeNode.root();
    

    Create and add your nodes (use your custom object as constructor param)

     TreeNode parent = new TreeNode("MyParentNode");
     TreeNode child0 = new TreeNode("ChildNode0");
     TreeNode child1 = new TreeNode("ChildNode1");
     parent.addChildren(child0, child1);
     root.addChild(parent);
    

    3) Add tree view to layout

     AndroidTreeView tView = new AndroidTreeView(getActivity(), root);
     containerView.addView(tView.getView());
    

    The simplest but not styled tree is ready. Now you can see parent node as root of your tree

    4) Custom view for nodes

    Extend TreeNode.BaseNodeViewHolder and overwrite createNodeView method to prepare custom view for node:

    public class MyHolder extends TreeNode.BaseNodeViewHolder<IconTreeItem> {
        ...
        @Override
        public View createNodeView(TreeNode node, IconTreeItem value) {
            final LayoutInflater inflater = LayoutInflater.from(context);
            final View view = inflater.inflate(R.layout.layout_profile_node, null, false);
            TextView tvValue = (TextView) view.findViewById(R.id.node_value);
            tvValue.setText(value.text);
            
            return view;
        }
        ...
        public static class IconTreeItem {
            public int icon;
            public String text;
        }
    }
    

    5) Connect view holder with node

      IconTreeItem nodeItem = new IconTreeItem();
      TreeNode child1 = new TreeNode(nodeItem).setViewHolder(new MyHolder(mContext));
    

    6) Consider using

    TreeNode.setClickListener(TreeNodeClickListener listener);
    AndroidTreeView.setDefaultViewHolder
    AndroidTreeView.setDefaultNodeClickListener
    ...
    

    For more details use sample application as example

    Let me know if i missed something, appreciate your support, thanks!

    相关文章

      网友评论

          本文标题:Android 树形结构(AndroidTreeView)

          本文链接:https://www.haomeiwen.com/subject/hryzvxtx.html