【问题标题】:3 level ExpandableListView Android3 级 ExpandableListView Android
【发布时间】:2017-03-06 13:41:48
【问题描述】:

我遇到了一些关于 SO 的问题,并且已经尝试过了。

我可以在展开式列表中显示最多 2 个级别,但是当我单击第二个级别时,我无法显示第三个级别。

我尝试过类似的方法:-

父视图(第一级)

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 级本身。

感谢任何帮助。

【问题讨论】:

  • 移动设备上的四级展开列表?我认为您可能会重新考虑您的设计,使其看起来更适合移动设备,例如 this
  • @R.Zagórski 客户有这样的要求,不能妥协:)

标签: android xamarin expandablelistview expandablelistadapter


【解决方案1】:

问题在于-->Second 的每一行都是一个完整的ExpandableListView,其中包含5 个组。并且由于当前父组的高度不会为子ExpandableListView 自动扩展。您看不到已打开的子列表: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;
    }
    

因此,它将正常工作:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-04
    • 1970-01-01
    • 2011-08-04
    • 1970-01-01
    相关资源
    最近更新 更多