Android Tree View

This is a simple to use Tree implementation for Android. This page will show you some examples for using the tree.

Include into your layout

First of all, you will need to include the Tree into your layout xml file. In the simplest case this may look like this.
	<?xml version="1.0" encoding="utf-8"?>
	<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	    android:layout_width="fill_parent"
	    android:layout_height="fill_parent"
	    android:orientation="vertical" 
	    >

	    <de.exware.android.ui.tree.Tree
                android:id="@+id/tree"
		android:layout_width="match_parent"
		android:layout_height="wrap_content" 
	    ></de.exware.android.tree.Tree>
	</LinearLayout>

Use as it is

Here we will show you how to use without any subclassing. As you can see, you simply need to create a structure of TreeObjects, and assign this to the Tree View. The default Renderer will use the toString() method of the supplied Object to be displayed.
   ITreeObject root = new TreeObject("Root");
   ITreeObject child = new TreeObject("Child 1");
   root.addChild(child);
   child = new TreeObject("Child 2");
   root.addChild(child);
   ITreeObject subchild = new TreeObject("Child 2.1");
   child.addChild(subchild);   
   Tree tree = (Tree) findViewById(R.id.tree);
   tree.setRoot(root);
The resulting Tree will look like this:

Handle user interaction

To get notififed if a user clicks on the tree, you may set an TreeListener by calling the Trees setTreeListener() method.

Use your own TreeObject class

If you have special needs, you may use your own TreeObject subclass or even implement the Interface ITreeObject completely by yourself. Here we will show you a simple subclass which changes the toString() method for customized rendering (This could also be done by subclassing the Tree as shown later).
   class MyTreeObject extends TreeObject<Person>
   {
      public MyTreeObject(Person person)
      {
         super(person);
      }

      public String toString()
      {
         return person.getName();
      }
   }

Use your own Tree subclass

To change the rendering completely you may create a subclass of tree as shown here. You will need to override the getView() method. This works like an ListViewAdapters getView() method.
    public View getView(View convertView, ITreeObject value)
    {
	DefaultRendererView view = super.getView(convertView, value);
        Person person = (Person)value;
        view.setText(person.getName());
        return view;
    }
The DefaultRendererView also shows the handles to open or close the Tree. If you want even more customization, you may avoid calling super.getView() and return your very own View.