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
. Android library that integrate sticky section headers in your RecyclerView

recyclerview-stickyheaders Recyclerview-stickyheaders is an Android library that makes it easy to integrate section headers in your RecyclerView. Thes

null 968 Nov 10, 2022
An Android custom ListView and ScrollView with pull to zoom-in.

PullZoomView An Android custom ListView and ScrollView with pull to zoom-in. Features Set ZoomView enable Add HeaderView Custom ZoomView Parallax or N

Frank-Zhu 2.3k Dec 26, 2022
An android library for section headers that stick to the top

StickyListHeaders StickyListHeaders is an Android library that makes it easy to integrate section headers in your ListView. These section headers stic

Emil Sjölander 5.5k Jan 2, 2023
Android library to achieve in an easy way, the behaviour of the home page in the Expedia app, with a pair of auto-scroll circular parallax ListViews.

ListBuddies This library is not maintained anymore and there will be no further releases Android library of a pair of auto-scroll circular parallax Li

JPARDOGO 970 Dec 29, 2022
Android library to observe scroll events on scrollable views.

Android-ObservableScrollView Android library to observe scroll events on scrollable views. It's easy to interact with the Toolbar introduced in Androi

Soichiro Kashima 9.6k Dec 29, 2022
Android ListView that mimics a GridView with asymmetric items. Supports items with row span and column span

AsymmetricGridView An Android custom ListView that implements multiple columns and variable sized elements. Please note that this is currently in a pr

Felipe Lima 1.8k Jan 7, 2023
Drag and drop GridView for Android

DynamicGrid Drag and drop GridView for Android. Depricated It's much better to use solutions based on recycler view. For example https://github.com/h6

Alex Askerov 920 Dec 2, 2022
An Android staggered grid view which supports multiple columns with rows of varying sizes.

AndroidStaggeredGrid ##Notice - Deprecated - 09-2015 This library has been deprecated. We will no longer be shipping any updates or approving communit

Etsy, Inc. 4.8k Dec 29, 2022
An Android Animation library which easily add itemanimator to RecyclerView items.

RecyclerView Animators RecyclerView Animators is an Android library that allows developers to easily create RecyclerView with animations. Please feel

Daichi Furiya 11.2k Jan 5, 2023
Android library providing simple way to control divider items (ItemDecoration) of RecyclerView

RecyclerView-FlexibleDivider Android library providing simple way to control divider items of RecyclerView Release Note [Release Note] (https://github

Yoshihito Ikeda 2.4k Dec 18, 2022
Android library defining adapter classes of RecyclerView to manage multiple view types

RecyclerView-MultipleViewTypeAdapter RecyclerView adapter classes for managing multiple view types Release Note [Release Note] (https://github.com/yqr

Yoshihito Ikeda 414 Nov 21, 2022
ItemDecoration for RecyclerView using LinearLayoutManager for Android

RecyclerItemDecoration RecyclerItemDecoration allows you to draw divider between items in recyclerview with multiple ViewType without considering item

magiepooh 328 Dec 27, 2022
Dividers is a simple Android library to create easy separators for your RecyclerViews

Dividers Dividers is an Android library to easily create separators for your RecyclerViews. It supports a wide range of dividers from simple ones, tha

Karumi 490 Dec 28, 2022
[UNMAINTAINED] Sticky Headers decorator for Android's RecyclerView

This project is no longer being maintained sticky-headers-recyclerview This decorator allows you to easily create section headers for RecyclerViews us

timehop 3.7k Dec 31, 2022
A modified version of Android's experimental StaggeredGridView. Includes own OnItemClickListener and OnItemLongClickListener, selector, and fixed position restore.

StaggeredGridView Introduction This is a modified version of Android's experimental StaggeredGridView. The StaggeredGridView allows the user to create

Maurycy Wojtowicz 1.7k Nov 28, 2022
An Android GridView that can be configured to scroll horizontally or vertically

TwoWayGridView An Android GridView that can be configured to scroll horizontally or vertically. I should have posted this over a year and a half ago,

Jess Anders 656 Jan 9, 2023
A drag-and-drop scrolling grid view for Android

DraggableGridView¶ ↑ a drag-and-drop scrolling grid view for Android Including in your project¶ ↑ To start using DraggableGridView: Place libs/Draggab

Tom Quinn 565 Dec 27, 2022
AndroidTreeView. TreeView implementation for android

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

Bogdan Melnychuk 2.9k Jan 2, 2023
Multi Roots TreeView implementation for Android Platform with a lot of options and customization

Multi roots TreeView :palm_tree: implementation for Android Platform with a lot of options and customization

Amr Hesham 112 Jan 3, 2023
Multi Roots TreeView implementation for Android Platform with a lot of options and customization

TreeView Multi Roots TreeView implementation for Android Platform with a lot of options and customization Demo Features: - No Custom Views. - Easy use

Amr Hesham 74 May 10, 2022