2017-08-31 5 views
0

ich ein NavigationView mit Checkbox oder Schaltern erstellen will, ich will nur diese Funktion in meiner Navigation haben, um Elemente wechseln und es sollte einfach so funktionieren: Wenn ich einen Schalter wenden ein anderes wechselt in den Off-Zustand. Was ich tat, um einen Schalter haben, ist dies:Java android NavigationView mit Checkbox

<?xml version="1.0" encoding="utf-8"?> 
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:orientation="horizontal" android:layout_width="match_parent" 
     android:layout_height="match_parent"> 

     <android.support.v7.widget.SwitchCompat 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:id="@+id/switchs" 
      /> 
    </LinearLayout> 

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

    <group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/nav_menu1" 
      android:icon="@mipmap/ic_launcher" 
      android:title="Menu 1" /> 
     <item 
      android:id="@+id/nav_menu2" 
      android:icon="@mipmap/ic_launcher" 
      android:title="Menu 2" /> 
     <item 
      android:id="@+id/nav_menu3" 
      app:actionLayout="@layout/action_view_switch" 
      android:icon="@mipmap/ic_launcher" 
      android:title="Menu 3" /> 
     <item 
      android:id="@+id/nav_menu4" 
      app:actionLayout="@layout/action_view_switch" 
      android:icon="@mipmap/ic_launcher" 
      android:title="Menu 4" /> 
    </group> 

</menu> 

Aber ich kann keinen Wert oder Status einen Schalter und einen Zuhörer haben ein Änderungsstand

Antwort

1

allererst erstellen ein Layout action_view_checkbox zu sehen. xml wie unten

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

       <TextView 
        android:layout_centerVertical="true" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_toLeftOf="@+id/checkbox" /> 

       <android.support.v7.widget.AppCompatCheckBox 
        android:id="@+id/checkbox" 
        android:layout_centerVertical="true" 
        android:layout_width="wrap_content" 
        android:layout_height="match_parent" 
        android:layout_alignParentRight="true" 
        android:text="" /> 
      </RelativeLayout> 

und fügen Sie das Layout als ein Element in das Menü

<menu xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:id="@+id/check_box_menu" 
     app:actionLayout="@layout/action_view_checkbox" 
     android:title="Title" /> 
</menu> 

und überprüfen c Leck Ereignis wie

Menu menu = navigationView.getMenu(); 
MenuItem menuItem = menu.findItem(R.id.check_box_menu); 
View actionView = MenuItemCompat.getActionView(menuItem); 
actionView.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 

    } 
}); 
+1

Aber wie kann ich überprüfen, ob dieses Kontrollkästchen ausgewählt ist oder nicht? –

+0

Ich habe versucht, aber versuchen Sie dies: ChechBox checkbox = (CheckBox) actionView.findViewById (R.id.checkbox); checkbox.setOnCheckChangeListner –

Verwandte Themen