0

Ich habe ein paar Fragen über SO in Bezug auf die gleichen und habe sie ausprobiert.3 Ebene ExpandableListView Android

Ich kann bis zu 2 Ebenen in der erweiterbaren Liste anzeigen, aber ich kann die dritte Ebene nicht anzeigen, wenn ich auf die zweite Ebene klicke.

Ich habe so etwas wie so versucht: -

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

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

}

Was ich festgestellt habe ist, dass die GetChildView() in der SecondLevelAdapter nie aufgerufen wird, obwohl die GetGroup() aufgerufen wird. Eigentlich möchte ich die Liste auf 4 Ebenen erweitern, aber ich bin auf der 3. Ebene festgefahren.

Jede Hilfe wird geschätzt.

+2

4-Ebene auf erweiterbare Liste Handy, Mobiltelefon? Ich denke, dass Sie Ihr Design überdenken könnten, um so mobiler zu sein wie [this] (https://docs.microsoft.com/en-us/windows/uwp/controls-and-patterns/images/patterns-md-stacked.png). –

+0

@ R.Zagórski gut der Kunde hat diese Art von Anforderung, die nicht kompromittiert werden kann :) – user3034944

Antwort

0

Das Problem ist für jede Zeile von -->Second ist es eine ganze ExpandableListView, die 5 Gruppen enthält. Und da die Höhe der aktuellen Elterngruppe nicht automatisch für das Kind ExpandableListView erweitert wird. Die Liste der geöffneten Kinder kann nicht angezeigt werden: third.

das Problem zu beheben, gibt es ein paar Änderungen vorgenommen werden:

  1. Ändern Sie den GroupCount von SecondLevelAdapter 5 bis 1:

    public override int GroupCount 
    { 
        get 
        { 
         //return 5; 
         return 1;//Change GroupCount to 1 
        } 
    } 
    
  2. Sie müssen die Höhe ändern jede zweite Ebene ExpandableListView manuell. Dies kann durch die Implementierung GroupExpand und GroupCollapse Ereignisse geschehen:

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

So wird es richtig:

enter image description here

+0

Danke, es hat gut funktioniert. – user3034944

Verwandte Themen