2016-03-19 21 views
0

Ich möchte Umschalttaste oder mindestens Kontrollkästchen in meinem NavigationView für die Funktion der Kontrolle Benachrichtigungen von ihm (ein/aus).Android NavigationView Menü Umschalttaste

Das Layout mit Hilfe der Datei "menu.xml" durchgeführt.

Ich habe versucht, checkable="true" hinzuzufügen, aber es zeigt nichts. Wie implementiert man es?

<group android:checkableBehavior="single"> 
     <item 
      android:id="@+id/profile" 
      android:icon="@drawable/ic_account_grey600_48dp" 
      android:title="@string/profile" /> 
     <item 
      android:id="@+id/tutorial" 
      android:icon="@drawable/ic_help_grey600_48dp" 
      android:title="@string/tutorial" /> 
<item 
    android:id="@+id/checkable_menu" 
    android:checkable="true" 
    android:title="@string/haircut_notifications" /> 

    </group> 

    <group 
     android:id="@+id/menu_bottom" 
     android:checkableBehavior="none"> 
     <item 
      android:id="@+id/nav_share" 
      android:icon="@drawable/ic_share_variant_grey600_48dp" 
      android:title="@string/share" /> 
     <item 
      android:id="@+id/selfie_videos" 
      android:icon="@drawable/ic_camera_front_variant_grey600_48dp" 
      android:title="@string/selfie_videos" /> 
    </group> 

</menu> 

EDIT:

Kleines Beispiel bei Google M mit Knebel wie ich erreichen möchte.

Haben Sie eine Idee, das zu tun?

enter image description here

+1

NavigationView ist für die Navigation innerhalb und App, so würde ich nicht erwarten, dass es für das Umschalten alles verwenden. Es ist jedoch üblich, Aktionsleistenmenüelemente als überprüfbare Umschaltelemente zu verwenden, die mehr Sinn ergeben. –

+0

@DougStevenson Ich habe gerade die Frage aktualisiert. Könnten Sie bitte das Bild noch einmal überprüfen, damit ich sicher sein kann, dass das kein guter Weg ist. –

+0

Wenn Sie mit dieser Implementierung zufrieden sind, dann gehen Sie auf jeden Fall mit. Es scheint unkonventionell, aber vielleicht ist das egal? –

Antwort

1

Sie könnten versuchen mit der App: actionLayout = "@ layout/item", item.xml ist, wo Sie Ihre Schaltfläche wechseln.

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto"> 

<item android:title="@string/settings_info"> 
     <menu> 

    <item android:id="@+id/nav_number" 
      android:icon="@drawable/ic_room" 
      android:title="@string/number" 
      app:actionLayout="@layout/item"/> 

    </item> 

</menu> 


/*How to handle the click event*/ 
@Override public boolean onNavigationItemSelected(MenuItem item) { 

// Handle navigation view item clicks. 
    switch (item.getItemId()){ 
     case R.id.nav_network: 
      startActivity(new Intent(this, NetworkActivity.class)); break; 
    } 

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout); 
    drawer.closeDrawer(GravityCompat.START); 
    return true; 
} 
+0

Danke! Das ist großartig! –

+0

Und vielleicht wissen Sie, wie man damit umgeht dann onclick genau auf diese App: actionLayout Artikel? –

+0

Sie sollten die Methode überschreiben onNavigationItemSelected, einen Blick auf dieses Beispiel geben: @Override public boolean onNavigationItemSelected (MenuItem Artikel) { // Handle Navigation Klicks Artikel ansehen. Schalter (item.getItemId()) { Fall R.id.nav_network: startActivity (new Intent (this, NetworkActivity.class)); Pause; } SchubladeLayout Schublade = (SchubladeLayout) findViewById (R.id.Drawer_layout); drawer.closeDrawer (GravityCompat.START); Rückkehr wahr; } –

2

statt Ansicht Verwendung Recycler Menü, so dass Sie viel mehr Kontrolle über die Elemente. Der folgende Code ist getestet und funktioniert.

Dies ist löschte: ScreenShot

Schritt eins: Artikel Layouts erstellen:

item_normal.xml

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

<ImageView 
    android:id="@+id/image" 
    android:layout_marginLeft="16dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@mipmap/ic_launcher" /> 

<TextView 
    android:id="@+id/text" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center_vertical" 
    android:layout_marginLeft="16dp" 
    android:text="Title" /> 

</LinearLayout> 

nichts Besonderes, einfach nur Bild und ein Text

Schritt 2 : Für unser Toggle-Layout können wir item_toggle.xml

erstellen

item_toggle.xml

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

<TextView 
    android:id="@+id/textCustom" 
    android:layout_marginLeft="16dp" 
    android:layout_centerVertical="true" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Notifications" /> 

<ToggleButton 
    android:layout_marginRight="16dp" 
    android:id="@+id/toggleButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentEnd="true" 
    android:layout_alignParentRight="true" 
    android:text="New ToggleButton" /> 
</RelativeLayout> 

Schritt 3: Damit können Adapter für unsere Recycler Ansicht erstellen, die in unserer Navigationsansicht

CustomAdapter.java

public class CustomAdapter extends RecyclerView.Adapter { 

String Tag = CustomAdapter.class.getSimpleName(); 

private final int NORMAL_VIEW = 88; // code for normal view type 
private final int CUSTOM_VIEW = 99; //code for loader type 

private Context context; 
private ArrayList<Model> listingData = new ArrayList<>(); 

public CustomAdapter(Context context) { 
    this.context = context; 
} 

@Override 
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

    LayoutInflater inflater = LayoutInflater.from(parent.getContext()); 

    if (viewType == NORMAL_VIEW) { 

     View normalView = inflater.inflate(R.layout.item_normal, parent, false); 
     return new NormalItem(normalView); 

    } else if (viewType == CUSTOM_VIEW) { 

     View customView = inflater.inflate(R.layout.item_toogle, parent, false); 
     return new CustomItem(customView); 
    } else return null; // unknown type. 
} 

@Override 
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { 

    if (holder instanceof NormalItem) { 
     // normal list item. 
     ((NormalItem) holder).textView.setText(listingData.get(position).getName()); 
     ((NormalItem) holder).icon.setImageResource(R.mipmap.ic_launcher); 

    } else if (holder instanceof CustomItem) 
    { 
     ((CustomItem) holder).textViewToggle.setText("On"); 
     ((CustomItem) holder).textViewToggle.setText("Notifications"); 
    } 
} 

@Override 
public int getItemCount() { 
    return listingData.size(); 
} 

@Override 
public int getItemViewType(int position) { 

    if (position == 3) return CUSTOM_VIEW; 
    else return NORMAL_VIEW; 
} 

public class NormalItem extends RecyclerView.ViewHolder { 

    ImageView icon; 
    TextView textView; 

    public NormalItem(View itemView) { 
     super(itemView); 

     icon = (ImageView) itemView.findViewById(R.id.image); 
     textView = (TextView) itemView.findViewById(R.id.text); 

    } 
} 

public class CustomItem extends RecyclerView.ViewHolder { 

    ToggleButton toggleButton; 
    TextView textViewToggle; 

    public CustomItem(View itemView) { 
     super(itemView); 

     toggleButton = (ToggleButton) itemView.findViewById(R.id.toggleButton); 
     textViewToggle = (TextView) itemView.findViewById(R.id.textCustom); 

    } 
} 


/** 
* method to update the adapter and notify the adapter item by item 
* 
* @param data data to be updated. 
*/ 
public void updateAdapter(ArrayList<Model> data) { 

    try { 
     for (Model item : data) { 
      listingData.add(item); 
      notifyItemInserted(getItemCount() - 1); 
     } 
    } catch (Exception e) { 
     Log.e(Tag, e.toString() + ""); 
    } 

} 
} 

Schritt 4 sein wird: ein erstellen activity_main.xml

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout    xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:fitsSystemWindows="true" 
tools:context=".main.MainActivity"> 


<android.support.v4.widget.DrawerLayout 
    android:id="@+id/drawerLayout" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:fitsSystemWindows="true" 
    tools:openDrawer="start"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:orientation="vertical"> 

     <include layout="@layout/toolbar" /> 

    </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigationView" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start"> 

     <android.support.v7.widget.RecyclerView 
      android:layout_marginTop="16dp" 
      android:id="@+id/recyclerView" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent" 
      android:scrollbars="vertical" /> 


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

Schritt 5: Ermöglicht eine einfache Klasse erstellen

Model.java unsere Daten

public class Model { 

public String getName() { 
    return name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

public int getItem() { 
    return item; 
} 

public void setItem(int item) { 
    this.item = item; 
} 

String name; 
int item; 
} 

Schritt 6 zu halten:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.v7.widget.Toolbar  xmlns:android="http://schemas.android.com/tools" 
xmlns:app="http://schemas.android.com/apk/res/android" 
app:id="@+id/toolbar" 
android:elevation="4dp" 
app:layout_width="match_parent" 
app:layout_height="wrap_content" 
app:background="?attr/colorPrimary" 
app:popupTheme="@style/ThemeOverlay.AppCompat.Light" 
app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar" 
android:background="?attr/colorPrimary" 
/> 

letzter Schritt: Erstellen MainActiivty .klasse

Hauptaktivität.Klasse

public class MainActivity extends AppCompatActivity { 

private DrawerLayout drawerLayout; 
private RecyclerView recyclerView; 
private ArrayList<Model> data; 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 
    getSupportActionBar().setTitle("Custom Nav "); 
    drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout); 
    NavigationView navigationView = (NavigationView) findViewById(R.id.navigationView); 

    ActionBarDrawerToggle mToogle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name); 
    drawerLayout.addDrawerListener(mToogle); 
    mToogle.syncState(); 

    recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
    LinearLayoutManager manager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(manager); 

    CustomAdapter adapter = new CustomAdapter(this); 
    recyclerView.setAdapter(adapter); 

    data = new ArrayList<>(); 

    Model item1 = new Model(); 
    item1.setName("Option 1"); 
    item1.setItem(R.mipmap.ic_launcher); 

    Model item2 = new Model(); 
    item2.setName("Option 2"); 
    item2.setItem(R.mipmap.ic_launcher); 

    Model item3 = new Model(); 
    item3.setItem(R.mipmap.ic_launcher); 
    item3.setName("Option 3"); 

    Model item4 = new Model(); 
    item4.setItem(R.mipmap.ic_launcher); 
    item4.setName("Option 4"); 


    data.add(item1); 
    data.add(item2); 
    data.add(item3); 
    data.add(item4); 
    adapter.updateAdapter(data); 
} 

} 

Sie können Header hinzufügen und die Benutzeroberfläche nach Ihren Bedürfnissen polieren. Ich hoffe es hilft. Glückliche Codierung.

+0

Danke! Sie würden diese Ansicht mit diesem Code erreichen und @ Arturo Mejia Antwort ist nur für die Frage, mit nur einem menu_item –

+0

ja, ich sah es, es ist besser, weniger Code. Wenn Sie mehr Kontrolle haben wollen, wie mehr Styling, könnten Sie dies verwenden. – Irfan

Verwandte Themen