7

Ich möchte eine Navigationsschublade erstellen können, die einige erweiterbare, auswählbare Elemente und einige nicht erweiterbare Elemente enthält. Der Konsens über StackOverflow für Fragen ähnlich wie mein Punkt zu Lösungen von ExpandableListView (die möglicherweise nicht einmal für meine Idee gelten). In den meisten Fällen, nach denen die Leute fragen, ist eine Möglichkeit, Elemente in der Nav-Schublade wie die GMail-App mit Etiketten zu trennen, nicht was ich zu tun versuche ...Wie erstelle ich ein Dropdown in einer Navigationsleiste (in Android)?

... die im Wesentlichen skizziert ist HERE enter image description here

und SIMILARLY HERE (though all, not some are dropdowns) . Und nicht wie THIS ANSWER.

Antwort

1

eine ExpandableListView innerhalb eines DrawerLayout verwenden, etwa so:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/drawer_layout2" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<FrameLayout 
    android:id="@+id/content_frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/tv_commentary_behind_nav" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal|top" 
     android:text="Frame text" /> 
</FrameLayout> 
<!-- The navigation drawer --> 
    <ExpandableListView 
     android:id="@+id/left_drawer2" 
     android:layout_width="250dp" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     android:background="@color/white" 
     android:choiceMode="multipleChoice" 
     android:dividerHeight="0dp" 
     /> 

</android.support.v4.widget.DrawerLayout> 

Dann im Code initialisiert wie folgt:

private DrawerLayout drawer; 
    private ExpandableListView drawerList; 
    private CheckBox checkBox; 
    private ActionBarDrawerToggle actionBarDrawerToggle; 


@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.drawer_layout); 
     drawer = (DrawerLayout) findViewById(R.id.drawer_layout2); 
     drawerList = (ExpandableListView) findViewById(R.id.left_drawer2); 
     drawerList.setAdapter(new NewAdapter(this, groupItem, childItem)); 
    } 
+1

Was sind 'groupItem' und' childItem'. Was ist NewAdapter und wie behandelt es 'groupItem' und' childItem', um einige Elemente erweiterbar zu machen? Deine Antwort ist unvollständig. –

+0

groupItem ist ein Array, das Strings für jede Gruppenoption enthält. "TopView 2" in der obigen Grafik. childItem ist eine Matrix/hashmap, die eine Liste von Elementen enthält, die nach dem Auswählen der einzelnen Gruppenelemente abgelegt werden sollen. NewAdapter ordnet die Werte von groupItem- und childItem-Datasets Ansichten zu, wenn der Halter jedes Elements aufgebläht wird. – AlleyOOP

1

Sie sollten eine Klasse haben alle implementierten Methoden der ExpandableListAdapter mit sowie ChildItemsInfo Klasse und eine GroupItemsInfo Klasse, mit MainActivity mit den Klick-Listener zu gruppieren Elemente und ihre Kinder

... jetzt genauer zu sein ...

du innerhalb getGroupView() platzieren können, die innerhalb MyExpandableListAdapter Klasse ist

View ind = convertView.findViewById(R.id.group_indicator); 
    View ind2 = convertView.findViewById(R.id.group_indicator2); 
    if (ind != null) 
    { 
     ImageView indicator = (ImageView) ind; 
     if (getChildrenCount(groupPosition) == 0) 
     { 
      indicator.setVisibility(View.INVISIBLE); 
     } 
     else 
     { 
      indicator.setVisibility(View.VISIBLE); 
      int stateSetIndex = (isExpanded ? 1 : 0); 

      /*toggles down button to change upwards when list has expanded*/ 
      if(stateSetIndex == 1){ 
       ind.setVisibility(View.INVISIBLE); 
       ind2.setVisibility(View.VISIBLE); 
       Drawable drawable = indicator.getDrawable(); 
       drawable.setState(GROUP_STATE_SETS[stateSetIndex]); 
      } 
      else if(stateSetIndex == 0){ 
       ind.setVisibility(View.VISIBLE); 
       ind2.setVisibility(View.INVISIBLE); 
       Drawable drawable = indicator.getDrawable(); 
       drawable.setState(GROUP_STATE_SETS[stateSetIndex]); 
      } 
     } 
    } 

... und wie sie für die Layout-Ansicht, das ist, wie meine group_items. xml erscheint

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="match_parent" 
android:layout_height="match_parent"> 

<TextView 
    android:id="@+id/group_heading" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:paddingLeft="20dp" 
    android:paddingTop="16dp" 
    android:paddingBottom="16dp" 
    android:textSize="15sp" 
    android:textStyle="bold"/> 

<ImageView 
    android:id="@+id/group_indicator" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@android:drawable/arrow_down_float" 
    android:layout_alignParentRight="true" 
    android:paddingRight="20dp" 
    android:paddingTop="20dp"/> 

<ImageView 
    android:id="@+id/group_indicator2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@android:drawable/arrow_up_float" 
    android:layout_alignParentRight="true" 
    android:visibility="gone" 
    android:paddingRight="20dp" 
    android:paddingTop="20dp"/> 

nicht klar genug sein ?, kommentieren, wenn