Android 带节头的可扩展ListView

Android 带节头的可扩展ListView,android,header,xamarin.android,expandablelistview,expandablelistadapter,Android,Header,Xamarin.android,Expandablelistview,Expandablelistadapter,我想创建一个带有节标题的可扩展列表视图。我尝试了以下示例中的两种示例方法 通过Commonware合并可扩展示例() 使用Jeff Sharkey()的标题分隔列表 我使用listView成功地实现了这些功能,但是,当我切换到使用expandableListView时,我遇到了一些问题: 当我试图实现Jeff的方法时,标题似乎碰巧有一个指示器,可以点击。当我想起来的时候,这似乎是不对的。标题不应该是expandableListView,或者至少不应该表现得像expandableListVi

我想创建一个带有节标题的可扩展列表视图。我尝试了以下示例中的两种示例方法

  • 通过Commonware合并可扩展示例()
  • 使用Jeff Sharkey()的标题分隔列表
我使用listView成功地实现了这些功能,但是,当我切换到使用expandableListView时,我遇到了一些问题:

  • 当我试图实现Jeff的方法时,标题似乎碰巧有一个指示器,可以点击。当我想起来的时候,这似乎是不对的。标题不应该是expandableListView,或者至少不应该表现得像expandableListView
  • 我还注意到,在为自定义expandableListAdapter实现时,我不再有IsEnabled()的函数。这可能是将“我的标题”启用为expandablelistview的原因。我考虑过我可能被IsChildSelectable()替换,但这不是我需要的(即启用/禁用组列表)
  • 我问过IRC频道的人,他们告诉我可能是我在包装部分遗漏了几个步骤,但我不能确定,所以我暂时抛出代码

    (顺便说一句,我使用的是Xamarin的monodroid。但是它的风格和流程与android差不多,所以我也理解android的代码,所以就在这里)

    独立扩展适配器类

    using System;
    using System.Collections.Generic;
    using Android.Content;
    using Android.Views;
    using Android.Widget;
    using Object = Java.Lang.Object;
    
    namespace AndroidApplication10
    {
        class SeparatedExpandableListAdapter :BaseExpandableListAdapter
        {
            public readonly Context context;
            public readonly Dictionary<string, IExpandableListAdapter> sections = new Dictionary<string, IExpandableListAdapter>();
            public readonly ArrayAdapter<string> headers;
            public static readonly int TYPE_SECTION_HEADER = 0;
    
            public SeparatedExpandableListAdapter(Context context)
            {
                this.context = context;
                headers = new ArrayAdapter<string>(context, Resource.Layout.list_header, Resource.Id.list_header_title);
            }
    
            //register our adapter with a section.
            public void AddSection(string section, IExpandableListAdapter adapter)
            {
                headers.Add(section);
                sections.Add(section, adapter);
            }
    
            /*
             * Algorithm for separatedList
             *      ::correctly find the selected item among child adapters::
             *          - subtract from the original position
             *          - do logic once the position find the header (position == 0)
             *          - do logic once the position find the adapter (position < size)
             */
    
            public override Object GetGroup(int groupPosition)
            {
                foreach (var section in sections)
                {
                    var size = section.Value.GroupCount + 1;
                    if (groupPosition == 0) return section.Key;
                    if (groupPosition < size) return section.Value.GetGroup(groupPosition - 1);
    
                    groupPosition -= size;
                }
                return null;
            }
            public override Object GetChild(int groupPosition, int childPosition)
            {
                foreach (var section in sections)
                {
                    var size = section.Value.GroupCount + 1;
                    if (groupPosition == 0) return null;
                    if (groupPosition < size) return section.Value.GetChild(groupPosition - 1, childPosition);
    
                    groupPosition -= size;
                }
                return null;
            }
    
            public override int GroupCount
            {
                get
                {
                    var total = 0;
                    foreach (var adapter in sections.Values)
                    {
                        total += adapter.GroupCount + 1;
                    }
                    return total;
                }
            }
            public override int GetChildrenCount(int groupPosition)
            {
                foreach (var section in sections)
                {
                    var size = section.Value.GroupCount + 1;
                    if (groupPosition == 0) return 0;
                    if (groupPosition < size) return section.Value.GetChildrenCount(groupPosition - 1);
    
                    groupPosition -= size;
                }
                return 0;
            }
            public override bool IsEmpty
            {
                get { return true; }
            }
    
            public override int GroupTypeCount
            {
                get
                {
                    var total = 1;
                    foreach (var adapter in sections.Values)
                    {
                        Console.WriteLine("[ROXEZ]: GroupTypeCount? " + ((BaseExpandableListAdapter)adapter).GroupTypeCount);
                        total += ((BaseExpandableListAdapter) adapter).GroupTypeCount; //?
                        Console.WriteLine("[ROXEZ]: total? " + total);
                    }
                    return total;
                }
            }
            public override int ChildTypeCount
            {
                get
                {
                    return base.ChildTypeCount;
                }
            }
    
            public override int GetGroupType(int groupPosition)
            {
                var type = 1;
                foreach (var section in sections)
                {
                    var size = section.Value.GroupCount + 1;
                    if (groupPosition == 0) return TYPE_SECTION_HEADER;
                    if (groupPosition < size)
                        return ((BaseExpandableListAdapter) (section.Value)).GetGroupType(groupPosition);
                    groupPosition -= size;
                    type += ((BaseExpandableListAdapter) (section.Value)).GroupTypeCount;
                }
                return -1;
            }
            public override int GetChildType(int groupPosition, int childPosition)
            {
                return base.GetChildType(groupPosition, childPosition);
            }
    
            public override bool AreAllItemsEnabled()
            {
                return false;
            }
            public override bool IsChildSelectable(int groupPosition, int childPosition)
            {
                    Console.WriteLine("Position: " + groupPosition + " | Output: " + (GetGroupType(groupPosition) != TYPE_SECTION_HEADER));
                    return (GetGroupType (groupPosition) != TYPE_SECTION_HEADER);
            }
    
            public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
            {
    
                var sectionNum = 0;
                foreach (var section in sections)
                {
                    var size = section.Value.GroupCount + 1;
                    if (groupPosition == 0)
                    {
                        return headers.GetView(groupPosition, convertView, parent);
                    }
                    if (groupPosition < size) return section.Value.GetGroupView(groupPosition - 1, isExpanded, convertView, parent);
    
                    groupPosition -= size;
                    sectionNum++;
                }
                return null;
            }
            public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
            {
                var sectionNum = 0;
                foreach (var section in sections)
                {
                    var size = section.Value.GroupCount + 1;
                    if (groupPosition == 0) return null;
                    if (groupPosition < size) return section.Value.GetChildView(groupPosition - 1, childPosition, isLastChild, convertView, parent);
    
                    groupPosition -= size;
                    sectionNum++;
                }
                return null;
            }
    
            public override long GetGroupId(int groupPosition)
            {
                return groupPosition;
            }
            public override long GetChildId(int groupPosition, int childPosition)
            {
                return childPosition;
            }
    
    
            public override bool HasStableIds
            {
                get { return true; }
            }
        }
    }
    
    使用系统;
    使用System.Collections.Generic;
    使用Android.Content;
    使用Android.Views;
    使用Android.Widget;
    使用Object=Java.Lang.Object;
    名称空间AndroidApplication 10
    {
    类分隔的ExpandableListAdapter:BaseExpandableListAdapter
    {
    公共只读上下文;
    公共只读词典节=新词典();
    公共只读ArrayAdapter标头;
    公共静态只读int TYPE_SECTION_HEADER=0;
    公共分隔的ExpandableListAdapter(上下文)
    {
    this.context=上下文;
    headers=new ArrayAdapter(上下文,Resource.Layout.list\u头,Resource.Id.list\u头\u标题);
    }
    //向节注册适配器。
    public void AddSection(字符串部分,IExpandableListAdapter适配器)
    {
    标题。添加(节);
    节。添加(节,适配器);
    }
    /*
    *分离列表算法
    *::在子适配器中正确找到所选项::
    *-从原始位置减去
    *-位置找到标题后执行逻辑(位置==0)
    *-位置找到适配器后执行逻辑(位置<尺寸)
    */
    公共覆盖对象GetGroup(int groupPosition)
    {
    foreach(节中的var节)
    {
    变量大小=section.Value.GroupCount+1;
    if(groupPosition==0)返回section.Key;
    if(groupPosition