AndroidTreeView. TreeView implementation for android

Overview

AndroidTreeView

Recent changes

2D scrolling mode added, keep in mind this comes with few limitations: you won't be able not place views on right side like alignParentRight. Everything should be align left. Is not enabled by default

Description

Tree view implementation for android

Android Arsenal

Demo

AndroidTreeView Demo on Google Play Store

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


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!

Projects using this library

Blue Dot : World Chat

Comments
  • Remove the default listener and handle Node expansion manually

    Remove the default listener and handle Node expansion manually

    I have tried to do treeView.setDefaultNodeClickListener(null); and then do this code whenever I'm creating a new node:

    taskNode.setClickListener(new TreeNode.TreeNodeClickListener() {
                @Override
                public void onClick(TreeNode treeNode, Object o) {
                    Intent openTaskDetails_Activity = new Intent(getActivity(), Task_ViewPager_Activity.class);
                    openTaskDetails_Activity.putExtra(Constants.GOD_TASK_OBJECT, Parcels.wrap(parentTask));
                    startActivity(openTaskDetails_Activity);
                }
            });
    

    But when I do this, both actions happen; The node expands/collapses and the item opens in a new activity, how can I disable the default node listener so that I can let the arrow button on the far right of my layout be responsible for expanding and collapsing items?

    opened by Odaym 9
  • Child node layout doesn't leave a gap on the left.

    Child node layout doesn't leave a gap on the left.

    Right now, my tree view looks like a normal list. The children don't move to the left. I had compared the project's sample but there isn't any different. What am I missing?

    opened by adamhongmy 6
  • Strange treeView.getView() returns null

    Strange treeView.getView() returns null

    This code runs through my parent Task objects and for each one of its children, it gets the children and adds them to that parent, then to the root, then again for all the remaining parents. At the end, it makes a new AndroidTreeView and set its properties and then should add that TreeView to the containerView I have defined (RelativeLayout cast into ViewGroup), just as your code says. processTasks(allTasks) is called only once.

    Seems strange that there would be an exception and I looked through all the issues, no one faced this..any help would be appreciated, thanks!

        public void processTasks(List<Task> allTasks) {
            final TreeNode root = TreeNode.root();
    
            for (Task task : allTasks) {
                TreeNode parentNode = new TreeNode(task).setViewHolder(new TaskTreeItemHolder(getActivity()));
                processTaskChildren(task, root, parentNode);
            }
    
            AndroidTreeView taskTreeView = new AndroidTreeView(getActivity(), root);
            taskTreeView.setDefaultAnimation(true);
            taskTreeView.setDefaultContainerStyle(R.style.TreeNodeStyleDivided, true);
    
            //Returns null here (Attempt to invoke virtual method 'android.content.Context android.view.View.getContext()' on a null object reference)
            // It's referring to the taskTreeView.getView() because naturally the exception would have mentioned the exception on addView instead
            taskListContainer.addView(taskTreeView.getView());
        }
    
        public void processTaskChildren(Task parentTask, TreeNode rootNode, TreeNode parentNode) {
            List<Task> childrenTasks = taskDAO.queryForEq("ParentTaskId", parentTask.getTask_id());
    
            for (Task child : childrenTasks) {
                TreeNode taskChildNode = new TreeNode(child).setViewHolder(new TaskTreeItemHolder(getActivity()));
                parentNode.addChild(taskChildNode);
                processTaskChildren(child, rootNode, parentNode);
            }
    
            rootNode.addChild(parentNode);
        }
    
    opened by Odaym 6
  • Implement selectable background

    Implement selectable background

    I need to display a background when I select a node like select element of ListView, I'm trying to set a background in nodeClickListener but doesn't work, any way to solve this? Thanks.

    opened by testica 6
  • [Question] TreeView in fragment

    [Question] TreeView in fragment

    I used to have my TreeView created in the main Activity. I move it to a Fragment but now nothing show up. I followed your samples but i can't find my mistake. I hope you can help me.

    Here is my Fragment creation:

    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.content_main, null, false);
            mViewRoot = (ViewGroup) view.findViewById(R.id.main_container);
    
            final TreeNode root = TreeNode.root();
            root.addChild(new TreeNode(new IconTreeItemHolder.IconTreeItem(R.string.ic_home, "Room")));
            atView = new AndroidTreeView(getActivity(), root);
            mViewRoot.addView(atView.getView());
    
            return view;
        }
    
    

    And here is my fragment layout:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/main_container"></RelativeLayout>
    
    
    </RelativeLayout>
    

    The fragment is added in the actvity xml layout.

    Thanks for help.

    opened by 3ffusi0on 5
  • Opened child nodes retain their state even if the parent is collapsed

    Opened child nodes retain their state even if the parent is collapsed

    Hi. Your library is important to our ongoing work requiring a sales model treeview. Please help me out on this one. Once I open a deep nested child within a node and then I collapse the node itself and reopen it, the child nodes maintain their opened state which is not required in our implementation.

    opened by mkjangid 4
  • Expose fields for subclass.

    Expose fields for subclass.

    Hi,

    The pull request changes are for the following reasons.

    1. I am currently writing a FileTreeView which has the interface

      
      public FileTreeView(Context context, File file) {
          super(context);
          TreeNode root = TreeNode.root();
          setUpNode(root, file);
          setRoot(root);
      }
      

      This requires creating a root node before initialising the superclass AndroidTreeView.

    2. mRoot usually needs to be visited in subclasses when writing a custom traverse method. I changed it to protected.

    3. I have the need to access the nodeContainer from others classes. For example, I want to set the background of a single row to a certain colour.

    this.highlightedNode.getViewHolder().getView().setBackgroundColor(color);;
    

    This would colour all the children nodes.

    Instead, I would like to do something like this:

    view = (TreeNodeWrapperView) this.highlightedNode.getViewHolder().getView();
    view.getNodeContainer().setBackgroundColor(color);
    
    opened by yehe01 4
  • Child Nodes Left Margin and setDisplayHomeAsUpEnabled not working

    Child Nodes Left Margin and setDisplayHomeAsUpEnabled not working

    I implemented the library on my android app, but i have 2 problems:

    • The child nodes doesn't have any left margin, so the tree looks like a simple list of itens.
    • And setDisplayHomeAsUpEnabled isnt working in the TreeNode fragment.

    This happened to someone ?

    opened by ghost 4
  • allowBackup:true set in Library?

    allowBackup:true set in Library?

    Hi,

    I'm running into a problem where my app has

     <application android:allowBackup="false" tools:replace="allowBackup" />
    

    There seems to be a Gradle/Android bug that causes that to fail because the AndroidTreeView Library has

     <application android:allowBackup="true" /> 
    

    Is there a reason this is set in the library manifest file itself? I was thinking maybe it wasn't necessary?

    Thanks for the great software!

    opened by dualbootsoftware 3
  • restore state issue

    restore state issue

    Right now on restoring the code takes the selected nodes and puts them in a hashset

    final Set<String> openNodes = new HashSet<>(Arrays.asList(openNodesArray));

    So if you have 1;0:0;2;0:0 gives you HashSet {1, 0:0, 2} but if you have 1;0:0;2 that gives you the same HashSet {1, 0:0, 2}

    There is data loss here because there is no indication which 0:0 is selected

    To see this in practice go to "Custom Holder Example" open "My Profile" -> Social open "Bruce Wayne" -> Social open "Barry Allen" -> Social; open "Barry Allen" -> Places rotate the device All the places nodes are expanded.

    I have a solution which looks like this and writes the tree with DFS as a string

        private void getSaveState(TreeNode root, StringBuilder sBuilder) {
            for (TreeNode node : root.getChildren()) {
                if (node.isExpanded()) {
                    sBuilder.append(NODE_EXPANDED);
                }
                else {
                    sBuilder.append(NODE_COLLAPSED);
                }
                getSaveState(node, sBuilder);
            }
        }
    
        public String getSaveState() {
            final StringBuilder builder = new StringBuilder();
            getSaveState(mRoot, builder);
            return builder.toString();
        }
    

    Let me know what you think of this solution, and I'll make a pull request with the changes.

    opened by fahimk 3
  • Indentation problem

    Indentation problem

    test code:

            root = TreeNode.root();
            TreeNode xd1 = new TreeNode("test1");
            TreeNode xd2 = new TreeNode("test2");
            TreeNode xd3 = new TreeNode("test3");
            TreeNode xd4 = new TreeNode("test4");
            TreeNode xd5 = new TreeNode("test5");
            xd4.addChildren(xd3, xd2);
            xd2.addChild(xd1);
            root.addChildren(xd4, xd5);
            tView = new AndroidTreeView(getActivity(), root);
            layout.addView(tView.getView());
    

    effect: screenshot_20180623-200257 Am i doing something wrong?

    opened by dumbasPL 2
  • how to compile this project with Android Studio Arctic Fox | 2020.3.1 Patch 3

    how to compile this project with Android Studio Arctic Fox | 2020.3.1 Patch 3

    I have run this project in my android studio. But it is not working properly due to build errors, can you please tell us how to compile with latest version or please give us an working sample in current android version.

    Thank you.

    opened by Velu-Venkat 0
  • AndroidTreeView with not showing padding for first level of hierarchy. Works for second level

    AndroidTreeView with not showing padding for first level of hierarchy. Works for second level

    I am using AndroidTreeView to show a hierarchy of items. First level of treeview is not being displayed with padding. the tree view like hierarchy is showing up from second level to all other levels. Bug explained in this video at https://www.youtube.com/watch?v=_9XjgVCqIOg&t=146

    If I remove the arrow icon, it shows good as expected with the correct padding for all the levels. I need arrow icon to indicate that the user can expand & collapse. https://github.com/onedrupal/One-Drupal-Android/blob/master/app/src/main/res/layout/layout_node.xml

    <com.github.johnkil.print.PrintView
            android:layout_alignParentLeft="true"
            android:id="@+id/arrow_icon"
    

    https://github.com/onedrupal/One-Drupal-Android/blob/master/app/src/main/java/treeutil/ArrowExpandSelectableHeaderHolder.java#L55

    arrowView = (PrintView) view.findViewById(R.id.arrow_icon);
            arrowView.setPadding(20,10,10,10);
            if (node.isLeaf()) {
                arrowView.setVisibility(View.INVISIBLE);
            }
    
    opened by ndubbaka 2
Owner
Bogdan Melnychuk
Bogdan Melnychuk
Wizard Pager is a library that provides an example implementation of a Wizard UI on Android, it's based of Roman Nurik's wizard pager (https://github.com/romannurik/android-wizardpager)

Wizard Pager Wizard Pager is a library that provides an example implementation of a Wizard UI on Android, it's based of Roman Nurik's wizard pager (ht

Julián Suárez 520 Nov 11, 2022
Material Design implementation for Android 4.0+. Shadows, ripples, vectors, fonts, animations, widgets, rounded corners and more.

Carbon Material Design implementation for Android 4.0 and newer. This is not the exact copy of the Lollipop's API and features. It's a custom implemen

null 3k Dec 30, 2022
Proof of concept Android WebView implementation based on Chromium code

Deprecation Notice This project is un-maintained. The recommended alternative is the Crosswalk Project. I did not have the time to keep the project up

Victor Costan 1.7k Dec 25, 2022
Wizard Pager is a library that provides an example implementation of a Wizard UI on Android

Wizard Pager is a library that provides an example implementation of a Wizard UI on Android, it's based of Roman Nurik's wizard pager.

Julián Suárez 520 Nov 11, 2022
Fully customizable implementation of "Snowfall View" on Android.

Android-Snowfall Fully customizable implementation of "Snowfall View" on Android. That's how we use it in our app Hotellook Compatibility This library

Jetradar Mobile 1.2k Dec 21, 2022
This is a sample Android Studio project that shows the necessary code to create a note list widget, And it's an implementation of a lesson on the Pluralsight platform, but with some code improvements

NoteKeeper-Custom-Widgets This is a sample Android Studio project that shows the necessary code to create a note list widget, And it's an implementati

Ibrahim Mushtaha 3 Oct 29, 2022
An implementation of tap targets from the Material Design guidelines for feature discovery.

TapTargetView An implementation of tap targets from Google's Material Design guidelines on feature discovery. Min SDK: 14 JavaDoc Installation TapTar

Keepsafe 5.2k Dec 30, 2022
Implementation of the fragment with the ability to display indeterminate progress indicator when you are waiting for the initial data.

Android-ProgressFragment Implementation of the fragment with the ability to display indeterminate progress indicator when you are waiting for the init

Evgeny Shishkin 813 Nov 11, 2022
A simple implementation of swipe card like StreetView

A simple implementation of swipe card like StreetView!! DONATIONS This project needs you! If you would like to support this project's further developm

Michele Lacorte 831 Jan 4, 2023
A library that provides an implementation of the banner widget from the Material design.

MaterialBanner A banner displays a prominent message and related optional actions. MaterialBanner is a library that provides an implementation of the

Sergey Ivanov 252 Nov 18, 2022
Android library used to create an awesome Android UI based on a draggable element similar to the last YouTube graphic component.

Draggable Panel DEPRECATED. This project is not maintained anymore. Draggable Panel is an Android library created to build a draggable user interface

Pedro Vicente Gómez Sánchez 3k Dec 6, 2022
TourGuide is an Android library that aims to provide an easy way to add pointers with animations over a desired Android View

TourGuide TourGuide is an Android library. It lets you add pointer, overlay and tooltip easily, guiding users on how to use your app. Refer to the exa

Tan Jun Rong 2.6k Jan 5, 2023
Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your development.

Bubbles for Android Bubbles for Android is an Android library to provide chat heads capabilities on your apps. With a fast way to integrate with your

Txus Ballesteros 1.5k Jan 2, 2023
View that imitates Ripple Effect on click which was introduced in Android L (for Android 2.3+)

RippleView View that imitates Ripple Effect on click which was introduced in Android L. Usage For a working implementation, Have a look at the Sample

Muthuramakrishnan Viswanathan 1.2k Dec 30, 2022
A new canvas drawing library for Android. Aims to be the Fabric.js for Android. Supports text, images, and hand/stylus drawing input. The library has a website and API docs, check it out

FabricView - A new canvas drawing library for Android. The library was born as part of a project in SD Hacks (www.sdhacks.io) on October 3rd. It is cu

Antwan Gaggi 1k Dec 13, 2022
MarkdownView is an Android webview with the capablity of loading Markdown text or file and display it as HTML, it uses MarkdownJ and extends Android webview.

About MarkdownView (Markdown For Android) is an Android library that helps you display Markdown text or files (local/remote) as formatted HTML, and st

Feras Alnatsheh 1k Dec 20, 2022
SwipeBack for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swipe gesture

SwipeBack SwipeBack is for Android Activities to do pretty the same as the android "back-button" will do, but in a really intuitive way by using a swi

Hannes Dorfmann 697 Dec 14, 2022
A backport of the SwitchPreference component that was introduced on Android 4 (ICS / level 14). This port works on Android 2.1+ (Eclair MR1 / level 7).

Android Switch Preference Backport A backport of the SwitchPreference component that was introduced on Android 4 (ICS / level 14). This port works on

Benoit Lubek 498 Dec 29, 2022