3级ExpandableListView Android [英] 3 level ExpandableListView Android

查看:67
本文介绍了3级ExpandableListView Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于SO,我遇到了几个问题,并进行了尝试.

I have come across a few questions on SO regarding the same and have tried them.

在展开式列表中最多可以显示2个级别,但是单击第二个级别时不能显示第三个级别.

I can show upto 2 levels in the expandable list, but I cannot show the third level when I click the second level.

我已经尝试过类似的方法:-

I have tried something like so :-

ParentView(第一级)

ParentView (First Level)

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

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);
}
}

SecondLevelAdapter

SecondLevelAdapter

   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;
    }
}

}

我注意到的是,尽管GetGroup()被调用,但从不调用SecondLevelAdapter中的GetChildView().我实际上想将列表扩展到4个级别,但我本身仍然停留在3个级别.

What I noticed is that the GetChildView() in the SecondLevelAdapter is never called though the GetGroup() is called. I actually want to expand the list upto 4 levels, but I am stuck at the 3rd level itself.

感谢您的帮助.

推荐答案

问题在于-->Second的每一行都是一个完整的ExpandableListView,其中包含5个组.并且由于当前父组的高度不会针对子ExpandableListView自动扩展.您看不到打开的子级列表:third.

The problem is for every row of -->Second it is a whole ExpandableListView,which contains 5 Groups. And as the height of the current parent group won't expand automatically for the child ExpandableListView. You can't see the opened children list: third.

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

To Fix the problem, there are a few changes to be made:

  1. SecondLevelAdapterGroupCount从5更改为1:

public override int GroupCount
{
    get
    {
        //return 5;
        return 1;//Change GroupCount to 1
    }
}

  • 您需要手动更改第二级ExpandableListView的高度.这可以通过实现GroupExpandGroupCollapse事件来完成:

  • You need to change the height of every second level ExpandableListView manually. This can be done by implementing GroupExpand and GroupCollapse events:

    ParentView.cs:

    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;
    }
    

  • 因此,它将正常工作:

    这篇关于3级ExpandableListView Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆