简体   繁体   English

Xamarin Android子方法从未在ExpandableListAdapter中调用

[英]Xamarin Android Child methods never called in ExpandableListAdapter

I'm having issues with ExpandableListAdapter. 我在ExpandableListAdapter中遇到问题。 I can't get the list to expand. 我无法扩大名单。 It seems the problem is with all of these child methods: Java.Lang.Object GetChild(int groupPosition, int childPosition) public override long GetChildId(int groupPosition, int childPosition) public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent) public override int GetChildrenCount(int groupPosition) 似乎所有这些子方法都有问题: Java.Lang.Object GetChild(int groupPosition, int childPosition) public override long GetChildId(int groupPosition, int childPosition) public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent) public override int GetChildrenCount(int groupPosition)

These methods are never being called. 这些方法永远不会被调用。 I can't seem to figure out why. 我似乎不知道为什么。 Here is my code: 这是我的代码:

ExpandableTaskListAdapter.cs: ExpandableTaskListAdapter.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace MyProject
{
    public class ExpandableTaskListAdapter : BaseExpandableListAdapter
    {
        private readonly Context context;
        private readonly List<string> headers;
        private readonly Dictionary<string, List<string>> listChildData;
        private readonly LayoutInflater inflater;
        private readonly bool[] expanded;

        public ExpandableTaskListAdapter(Context context, List<string> headers, Dictionary<string, List<string>> listChildData, LayoutInflater inflater)
        {
            this.context = context;
            this.headers = headers;
            this.listChildData = listChildData;
            this.inflater = inflater;
            expanded = new bool[headers.Count];
        }

        public override void OnGroupCollapsed(int groupPosition)
        {
            expanded[groupPosition] = !expanded[groupPosition];
            base.OnGroupCollapsed(groupPosition);
        }

        public override void OnGroupExpanded(int groupPosition)
        {
            expanded[groupPosition] = !expanded[groupPosition];
            base.OnGroupExpanded(groupPosition);
        }

        public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
        {
            return listChildData[headers[groupPosition]][childPosition];
        }

        public override long GetChildId(int groupPosition, int childPosition)
        {
            return childPosition;
        }

        public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
        {
            string childText = (string)GetChild(groupPosition, childPosition);
            convertView = inflater.Inflate(Resource.Layout.task_child, null);

            TextView listChild = convertView.FindViewById<TextView>(Resource.Id.label);
            listChild.Text = childText;

            return convertView;
        }

        public override int GetChildrenCount(int groupPosition)
        {
            return listChildData[headers[groupPosition]].Count;
        }

        public override Java.Lang.Object GetGroup(int groupPosition)
        {
            return headers[groupPosition];
        }

        public override long GetGroupId(int groupPosition)
        {
            return groupPosition;
        }

        public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {
            string headerTitle = (string)GetGroup(groupPosition);

            convertView = inflater.Inflate(Resource.Layout.task_child, null);

            TextView header = convertView.FindViewById<TextView>(Resource.Id.label);
            header.Text = headerTitle;

            convertView.FindViewById<LinearLayout>(Resource.Id.main_layout).Click += (sender, e) =>
            {
                if (expanded[groupPosition])
                    OnGroupCollapsed(groupPosition);
                else
                    OnGroupExpanded(groupPosition);
            };

            return convertView;
        }

        public override int GroupCount
        {
            get { return headers.Count; }
        }

        public override bool HasStableIds
        {
            get { return true; }
        }

        public override bool IsChildSelectable(int groupPosition, int childPosition)
        {
            return true;
        }
    }
}

task_child.xml: task_child.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">

  <TextView
    android:id="@+id/label"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:textSize="17sp"
    android:paddingLeft="40dp"/>

  <ImageView
    android:id="@+id/icon"
    android:layout_width="30dp"
    android:layout_height="30dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="5dp"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"
    android:background="@drawable/ic_delete"/>

  <CheckBox
    android:id="@+id/checkbox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:layout_gravity="center_vertical"/>

</LinearLayout>

Putting a breakpoint in the constructor, I can see that I am passing in the correct data. 在构造函数中放置一个断点,可以看到我正在传递正确的数据。 Why are the child methods never being called? 为什么从不调用子方法?

When your GroupHeaderView is clicked the ExpandableListView invokes the OnGroupExpanded where the expanded[position] = true is set. 当你的GroupHeaderView被点击ExpandableListView调用OnGroupExpandedexpanded[position] = true设置。

However, your .Click subscriber on the ListView in the GroupHeaderView is also called, where it finds the expanded[position] == true and calls the OnGroupCollapsed method. 然而,你的.Click上的GroupHeaderView ListView的用户也被称为,在那里找到了expanded[position] == true ,并调用OnGroupCollapsed方法。 This results in the Group not being expanded at all and no requests for it's children are made. 这导致该组根本无法扩展,也没有要求其子组。

Just remove the .Click handler in GetGroupView method and everything should work fine. 只需删除GetGroupView方法中的.Click处理程序,一切就可以正常工作。

    public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
    {
        string headerTitle = (string)GetGroup(groupPosition);

        convertView = inflater.Inflate(Resource.Layout.task_child, null);

        TextView header = convertView.FindViewById<TextView>(Resource.Id.label);
        header.Text = headerTitle;

        return convertView;
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM