3 级 ExpandableListView Android

3 level ExpandableListView Android

我在 SO 上遇到了一些关于相同问题的问题并尝试了这些问题。

我在可扩展列表中最多可以显示2级,但是当我点击第二级时我无法显示第三级。

我试过这样的事情:-

ParentView(一级)

public class ParentView : BaseExpandableListAdapter
    {
        public static int FIRST_LEVEL_COUNT = 6;
        public static int SECOND_LEVEL_COUNT = 4;
        public static int THIRD_LEVEL_COUNT = 10;
        private Context context;



        public ParentView(Context context)
        {
            this.context = context;
        }

        public ParentView()
        {
        }

        public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
        {
            return 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)
        {
            var SecondLevelexplv = new CustExpListview(Application.Context);
            SecondLevelexplv.SetAdapter(new SecondLevelAdapter(context));
            SecondLevelexplv.SetGroupIndicator(null);
            return SecondLevelexplv;
        }

        public override int GetChildrenCount(int groupPosition)
        {
            return 3;
        }

        public override Java.Lang.Object GetGroup(int groupPosition)
        {
            return groupPosition;
        }
        public override int GroupCount
        {
            get
            {
                return 5;
            }
        }

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

        public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
        {
            var tv = new TextView(Application.Context)
            {
                Text = "->FirstLevel",
            };
            tv.SetBackgroundColor(Color.Blue);
            tv.SetPadding(10, 7, 7, 7);

            return tv;
        }

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

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

    }

CustExpListView

public class CustExpListview : ExpandableListView
{

public CustExpListview(Context context) : base(context)
{
}

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    widthMeasureSpec = MeasureSpec.MakeMeasureSpec(999999, MeasureSpecMode.AtMost);
    heightMeasureSpec = MeasureSpec.MakeMeasureSpec(999999, MeasureSpecMode.AtMost);
    OnMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

二级适配器

   public class SecondLevelAdapter : BaseExpandableListAdapter
{
public static int THIRD_LEVEL_COUNT = 10;
private Context context;
private readonly ParentView parentView;

public SecondLevelAdapter(Context context) 
{
    this.context = context;
}

public SecondLevelAdapter(ParentView parentView)
{
this.parentView = parentView;
}

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

public override int GroupCount
{
    get
    {
        return 5;
    }
}



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


public override View GetGroupView(int groupPosition, bool isExpanded, View convertView, ViewGroup parent)
{
    var tv = new TextView(Application.Context)
    {
        Text = "-->Second"
    };
    tv.SetPadding(12, 7, 7, 7);
    tv.SetBackgroundColor(Color.GreenYellow);

    return tv;
}



public override Java.Lang.Object GetChild(int groupPosition, int childPosition)
{
    return 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)
{
    var tv = new TextView(Application.Context)
    {
        Text = "third"
    };
    tv.SetPadding(18, 5, 5, 5);
    tv.SetBackgroundColor(Color.Green);

    return tv;
}


public override int GetChildrenCount(int ChildPosition)
{
    return 4;
}


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

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

}

我注意到 SecondLevelAdapter 中的 GetChildView() 从未被调用,尽管 GetGroup() 被调用了。我实际上想将列表扩展到 4 个级别,但我自己卡在了第 3 个级别。

感谢任何帮助。

问题是 -->Second 的每一行都是一个整体 ExpandableListView,其中包含 5 个组。由于当前 parent 组的高度不会自动扩展 child ExpandableListView。看不到打开的children列表:third.

要解决此问题,需要进行一些更改:

  1. SecondLevelAdapterGroupCount从5改为1:

    public override int GroupCount
    {
        get
        {
            //return 5;
            return 1;//Change GroupCount to 1
        }
    }
    
  2. 您需要手动更改每个第二级 ExpandableListView 的高度。这可以通过实施 GroupExpandGroupCollapse 事件来完成:

    ParentView.cs:

    public override View GetChildView(int groupPosition, int childPosition, bool isLastChild, View convertView, ViewGroup parent)
    {
        var SecondLevelexplv = new CustExpListview(Application.Context);
        SecondLevelexplv.SetAdapter(new SecondLevelAdapter(context));
        SecondLevelexplv.SetGroupIndicator(null);
        SecondLevelexplv.GroupExpand += SecondLevelexplv_GroupExpand;
        SecondLevelexplv.GroupCollapse += SecondLevelexplv_GroupCollapse;
        return SecondLevelexplv;
    }
    
    private void SecondLevelexplv_GroupCollapse(object sender, ExpandableListView.GroupCollapseEventArgs e)
    {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MatchParent, 70);
        (sender as CustExpListview).LayoutParameters = lp;
    }
    
    private void SecondLevelexplv_GroupExpand(object sender, ExpandableListView.GroupExpandEventArgs e)
    {
        //expand the group and the height is the `children count` * `unit height`
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MatchParent, 70 * 5);
        (sender as CustExpListview).LayoutParameters = lp;
    }
    

因此,它将正常工作: