Reputation: 133
I am trying to develop a TreeView in xamarin android.I followed this Link to develop the below sample.
I do not know how to change the foreground color of TreeNode i.e parent and child1 which are part of Linear Layout.
I am able to change the color of TreeItems cause I am displaying the TreeItems in TextView.
Any help is highly appreciated.
My MainActivity.cs:
namespace XamarinTreeView
{
[Activity(Label = "XamarinTreeView", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//create root
TreeNode root = TreeNode.InvokeRoot();
TreeNode parent = new TreeNode("parent2");
///Parent2 root children
TreeNode child1 = new TreeNode("Child1");
TreeItem item = new TreeItem() { text = " Level1" };
TreeNode child10 = new TreeNode(item).SetViewHolder(new MyHolder(this));
child1.AddChild(child10);
TreeItem item1 = new TreeItem() { text = " Level2" };
TreeNode child11 = new TreeNode(item1).SetViewHolder(new MyHolder(this));
child10.AddChild(child11);
TreeItem item2 = new TreeItem() { text = " Level2-1" };
TreeNode child111_1 = new TreeNode(item2).SetViewHolder(new MyHolder(this));
child11.AddChild(child111_1);
TreeItem item3 = new TreeItem() { text = " Level2-2" };
TreeNode child111_2 = new TreeNode(item3).SetViewHolder(new MyHolder(this));
child11.AddChild(child111_2);
///
parent.AddChildren(child1);
root.AddChild(parent);
AndroidTreeView atv = new AndroidTreeView(this, root);
LinearLayout rootlayout = FindViewById<LinearLayout>(Resource.Id.rootlayout);
rootlayout.AddView(atv.View);
rootlayout.Invalidate();
}
}
public class TreeItem : Java.Lang.Object
{
public string text;
}
}
Main.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootlayout">
</LinearLayout>
Upvotes: 0
Views: 133
Reputation: 4358
Do what you have done on child10
:
namespace XamarinTreeView
{
[Activity(Label = "XamarinTreeView", MainLauncher = true)]
public class MainActivity : Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.Main);
//create root
TreeNode root = TreeNode.InvokeRoot();
TreeItem parent_item = new TreeItem() { text = "parent2" };
TreeNode parent = new TreeNode(parent_item).SetViewHolder(new MyHolder(this));
///Parent2 root children
TreeItem child1_item = new TreeItem() { text = "Child1" };
TreeNode child1 = new TreeNode(child1_item).SetViewHolder(new MyHolder(this));
TreeItem item = new TreeItem() { text = " Level1" };
TreeNode child10 = new TreeNode(item).SetViewHolder(new MyHolder(this));
child1.AddChild(child10);
TreeItem item1 = new TreeItem() { text = " Level2" };
TreeNode child11 = new TreeNode(item1).SetViewHolder(new MyHolder(this));
child10.AddChild(child11);
TreeItem item2 = new TreeItem() { text = " Level2-1" };
TreeNode child111_1 = new TreeNode(item2).SetViewHolder(new MyHolder(this));
child11.AddChild(child111_1);
TreeItem item3 = new TreeItem() { text = " Level2-2" };
TreeNode child111_2 = new TreeNode(item3).SetViewHolder(new MyHolder(this));
child11.AddChild(child111_2);
///
parent.AddChildren(child1);
root.AddChild(parent);
AndroidTreeView atv = new AndroidTreeView(this, root);
LinearLayout rootlayout = FindViewById<LinearLayout>(Resource.Id.rootlayout);
rootlayout.AddView(atv.View);
rootlayout.Invalidate();
}
}
public class TreeItem : Java.Lang.Object
{
public string text;
}
}
Upvotes: 1
Reputation: 21
Create a new class to represent the TreeNode like you have for TreeItem. Create a new holder like you have for TreeItem (MyHolder.cs). Create a new layout like you have for TreeItem (itemview.axml)
MyNodeHolder.cs
public class MyNodeHolder : TreeNode.BaseNodeViewHolder
{
private Context mcontext;
public MyNodeHolder(Context context) : base(context)
{
mcontext = context;
}
public override View CreateNodeView(TreeNode p0, Java.Lang.Object p1)
{
var inflater = LayoutInflater.FromContext(mcontext);
var view = inflater.Inflate(Resource.Layout.nodeview, null, false);
TextView tv = view.FindViewById<TextView>(Resource.Id.nodetv);
var item = p1 as TreeNodeItem;
tv.Text = item.text;
return view;
}
}
nodeview.axml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/darker_gray">
<TextView
android:id="@+id/nodetv"
android:layout_height="wrap_content"
android:layout_width="match_parent" />
</LinearLayout>
Create your node in the activity:
streetNode.AddChild(streetNumberNode.SetViewHolder(new MyNodeHolder(this)));
I hope it helps.
Upvotes: 2