2017-03-06 106 views
0

我遇到过几个关于同样的问题,并尝试过它们。3级ExpandableListView Android

我可以在可扩展列表中显示最多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); 
} 
} 

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个级别,但是我被困在了第三级别。

任何帮助表示赞赏。

+2

4级扩展列表移动?我认为你可能会重新考虑你的设计,以便像[this]一样更具移动友好性(https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/images/patterns-md-stacked.png )。 –

+0

@R.Zagórski以及客户有这样的要求,不能妥协:) – user3034944

回答

0

问题是对于-->Second的每一行它是一个整体ExpandableListView,它包含5个组。并且由于当前父组的高度不会自动扩大,因此子ExpandableListView。您看不到打开的儿童列表:third

要解决这个问题,要作出一些改动:

  1. 变化SecondLevelAdapter从5 GroupCount以1:

    public override int GroupCount 
    { 
        get 
        { 
         //return 5; 
         return 1;//Change GroupCount to 1 
        } 
    } 
    
  2. 您需要更改的高度手动每隔二级展开一次ListView。

    ParentView.cs:这可以通过实施GroupExpandGroupCollapse事件进行

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

因此,将正常工作:在

enter image description here

+0

谢谢它工作正常。 – user3034944