You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin Android TreeView点击父节点后子节点不显示问题求助

Fixing Child Nodes Not Showing After Parent Node Click in Xamarin Android TreeView

Hey there! I see exactly what's going on with your TreeView—when you click the parent node, you're only updating the icon but not actually telling the TreeView to expand/collapse the node itself. That's why the child levels aren't appearing. Let's fix this step by step.

The Root Issue

Your ParentView_Click method only modifies the drawable icon, but it doesn't trigger the TreeNode's core state change. The TreeView relies on the TreeNode's expanded/collapsed status to know whether to render its child nodes, so we need to sync that state and update the UI properly.

Updated Solution Code

Here's how to adjust your MyHolder.cs to get child nodes showing correctly:

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 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;
 
 // Set initial icon based on the node's default expanded state
 int initialDrawable = Node.IsExpanded ? Resource.Drawable.minus : Resource.Drawable.plus;
 parent_View.SetCompoundDrawablesWithIntrinsicBounds(initialDrawable, 0, 0, 0);

 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 {
 var view = inflater.Inflate(Resource.Layout.itemview, null, true);
 level2_View = view.FindViewById<TextView>(Resource.Id.itemtv);
 level2_View.Text = item.text;
 return view;
 }
 }

 private void ParentView_Click(object sender, EventArgs e) {
 // Toggle the node's expand/collapse state (this triggers TreeView to update child nodes)
 Node.Toggle();

 // Update the icon to match the new state, using the clicked TextView directly
 var clickedTextView = sender as TextView;
 int drawableId = Node.IsExpanded ? Resource.Drawable.minus : Resource.Drawable.plus;
 clickedTextView.SetCompoundDrawablesWithIntrinsicBounds(drawableId, 0, 0, 0);
 }
 }
}

Key Changes Explained

  1. Initial Icon Sync: When creating the parent node view, we now set the correct starting icon (plus for collapsed, minus for expanded) based on the node's default state.
  2. Trigger Node State Change: Calling Node.Toggle() tells the TreeView to switch the node's status, which automatically triggers the rendering or hiding of child nodes—this is the missing piece in your original code.
  3. Targeted UI Update: Instead of referencing parent_View directly (which could point to the wrong node if multiple parents exist), we use the sender parameter to update the icon of the exact node that was clicked.

Quick Double-Check

Make sure you've properly added child nodes to your parent TreeNodes when building the TreeView structure. If a parent node has no children assigned, toggling its state won't show anything—verify that part of your setup too!

内容的提问来源于stack exchange,提问作者Rachel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 09:33:19