DEV Community

Cover image for A new Android TreeView implementation
Amr Hesham
Amr Hesham

Posted on

A new Android TreeView implementation

Hello everyone, at the start of 2022 I want to create a files tree as you see in any code editor or IDE that represents the project structures and you can do some operations like expanding or collapsing directories, and I have searched for many Tree View implementations in GitHub but I found that some of them are not maintained from many years and also the example project didn’t run on new Android Studio versions, other implementations create custom views from scratch and draw the tree but they didn’t work correctly with a big number of nodes and other limitations.

So I started to create a list that contains a set of features from all of them, and also a set of features that were requested on the GitHub Issues to get inspiration from them so I can create a generic and useful implementation that can be easy to customize and extend by everyone and as a result of this I want to introduce This new TreeView library.

The main goal is to make TreeView easy to use, customize and extend almost without limitation and to be up to date with the new android versions, so what you can do with TreeView?

Image description

TreeView didn’t have any custom view, all you need is a normal RecyclerView so you can get the benefits of RecyclerView recycling and performance, also you can get 2D Scrolling without any custom layout, think of it like you want to make your normal RecyclerView have 2D Scrolling, TreeView has a custom RecyclerView Adapter with click and long click listeners ready to use and we can add more in the future, so each TreeNode didn’t need to have a reference for it listeners.

You can have any number of roots, and also any number of list items layouts without the need to extend and override the default adapter, I have implemented using the Factory design pattern so you need to pass a TreeViewHolderFactory to the adapter constructor that maps each list item layout with it ViewHolder, so all you need is to create a View Holder for this layout and add it to the factory.

Image description

TreeView also supports many operations like expanding and collapsing nodes and branches or by levels also has expanding and collapsing all nodes, you can also get the currently selected node and the parent for any node, all of those features implementation is encapsulated in the TreeNodeManager class so we can easily write a unit tests for them, also if you want to have different implementation you can easily create a custom TreeNodeManager and pass it to the adapter.

All of the details about how to install, use and extend are written on the TreeView website with examples and if you face any problems or have any questions, please feel free to create a new issue on the Github repository so we can help you quickly.

Image description

This implementation is ready to use and if you have any ideas or requirements feel free to share them or contribute and add them, also you will find all details on the website.

TreeView Github: AmrDeveloper/TreeView
TreeView Website: Github.io/TreeView

You can find me on: GitHub, LinkedIn, Twitter.

Enjoy Programming 😋.

Top comments (0)