TreeView中的EventHandling(Xamarin android)

时间:2018-05-14 07:12:19

标签: xamarin.android treeview treenode

我在xamarin android中开发了一个树视图。我跟着这个Link来开发下面的示例。

在MyHolder.cs中有一个名为CreateNodeView()的方法,它显示所有节点。 在这里,当我点击ParentNode时,控件将转到ParentView_Click()[Event Handler]。 我面临的问题是从ParentView_Click()返回后,ParentNode下面的级别没有显示。

非常感谢任何帮助。

MyHolder.cs:

namespace XamarinTreeView
{
public class MyHolder : TreeNode.BaseNodeViewHolder
{

    private Context mcontext;
    TextView level2_View, child_View, parent_View;

    public MyHolder(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.itemview, null, true);

        level2_View = view.FindViewById<TextView>(Resource.Id.itemtv);
        child_View = view.FindViewById<TextView>(Resource.Id.itemtv1);
        parent_View = view.FindViewById<TextView>(Resource.Id.parent_tv);
        // tv.Click += Tv_Click;


        var item = p1 as TreeItem;
        if (item.text == " ParentNode" || item.text == " Child1")
        {
            var view2 = inflater.Inflate(Resource.Layout.parentview, null, true);
            parent_View = view2.FindViewById<TextView>(Resource.Id.parent_tv);
            parent_View.Text = item.text;
            parent_View.Click += ParentView_Click;             
            return view2;
        }
        if (item.text == " Level3-1" || item.text == " Level3-2" || item.text == " Level4")
        {
            var view1 = inflater.Inflate(Resource.Layout.childview, null, true);
            child_View = view1.FindViewById<TextView>(Resource.Id.itemtv1);
            child_View.Text = item.text;
            return view1;
        }
        else
        {
            level2_View.Text = item.text;
            return view;
        }
    }

    private void ParentView_Click(object sender, EventArgs e)
    {
        int iLeft = Resource.Drawable.plus;
        parent_View.SetCompoundDrawablesWithIntrinsicBounds(iLeft, 0, 0, 0);
    }      
 }
}

1 个答案:

答案 0 :(得分:0)

如果您想在Node上添加点击事件,可以使用TreeNode.setClickListener(TreeNodeClickListener listener);

为您TreeNode.ITreeNodeClickListener实施MainActivity

 public void OnClick(TreeNode p0, Object p1)
        {
            //do something
        var view = parent.ViewHolder.View as View;
        int iLeft = Resource.Drawable.dapao;
        TextView tv = view.FindViewById<TextView>(Resource.Id.itemtv);
        tv.SetCompoundDrawablesWithIntrinsicBounds(iLeft, 0, 0, 0);
        tv.Text = "adfasdf";
        Toast.MakeText(this, "111", ToastLength.Short).Show();
        }

OnCreate方法中添加监听器:

parent.SetClickListener(this);

请参阅this

我还提供了github

的演示