TreeView:利用 RecyclerView 打造高性能树形控件


An android tree view with high performance and rich functions


There are some gifs from Demo:

Select node:

Operate all:


expandAll/collapseAll expandNode/collapseNode expandLevel/collapseLevel toggleNode deleteNode/addNode selectNode/deselectNode selectAll/deselectAll getAllSelectedNodes


1.Reference the library from your module's build.gradle:

compile 'me.texy.treeview:treeview_lib:1.0.2'

2.Implement your all level's BaseNodeViewBinder


public class FirstLevelNodeViewBinder extends BaseNodeViewBinder {  TextView textView;  public FirstLevelNodeViewBinder(View itemView) {     super(itemView);      textView = (TextView) view.findViewById(  }    @Override  public int getLayoutId() {    return R.layout.item_first_level;  }    @Override  public void bindView(TreeNode treeNode) {    textView.setText(treeNode.getValue().toString());  }}SecondLevelNodeViewBinderThirdLevelNodeViewBinder...

If you want add the selectable feature,replace BaseNodeViewBinder with CheckableNodeViewBinder.

3.Implement the BaseNodeViewFactory


public class MyNodeViewFactory extends BaseNodeViewFactory {  @Override  public BaseNodeViewBinder getNodeViewBinder(View view, int level) {    switch (level) {      case 0:        return new FirstLevelNodeViewBinder(view);      case 1:        return new SecondLevelNodeViewBinder(view);      case 2:        return new ThirdLevelNodeViewBinder(view);      default:        return null;    }  }}

If you do not want to create a class file,just implement a anonymous inner class in TreeView's constructor

4.Add TreeView to wherever you want


TreeNode root = TreeNode.root();//build the tree as you wantfor (int i = 0; i < 5; i++) {  TreeNode treeNode = new TreeNode(new String("Child " + "No." + i));  treeNode.setLevel(0);  root.addChild(treeNode);}View treeView = new TreeView(root, context, new MyNodeViewFactory()).getView();//add to view group where you want


Copyright 2017 shineM.

Licensed under the Apache License, Version 2.0 (the "License");you may not use this file except in compliance with the License.You may obtain a copy of the License at required by applicable law or agreed to in writing, softwaredistributed under the License is distributed on an "AS IS" BASIS,WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.See the License for the specific language governing permissions andlimitations under the License.

本文由 华盟网 作者:小龙 发表,其版权均为 华盟网 所有,文章内容系作者个人观点,不代表 华盟网 对观点赞同或支持。如需转载,请注明文章来源。