hi have navigation drawer in android application in main activity java class , code
public class mainactivity extends appcompatactivity implements navigationview.onnavigationitemselectedlistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar); drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); actionbardrawertoggle toggle = new actionbardrawertoggle( this, drawer, toolbar, r.string.navigation_drawer_open, r.string.navigation_drawer_close); drawer.setdrawerlistener(toggle); toggle.syncstate(); navigationview navigationview = (navigationview) findviewbyid(r.id.nav_view); navigationview.setnavigationitemselectedlistener(this); //add line display menu1 when activity loaded displayselectedscreen(r.id.nav_menu1); } @override public void onbackpressed() { drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawer(gravitycompat.start); } else { super.onbackpressed(); } } private void displayselectedscreen(int itemid) { //creating fragment object fragment fragment = null; drawerlayout drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); //initializing fragment object selected switch (itemid) { case r.id.nav_menu1: fragment = new gallary(); break; case r.id.nav_menu2: fragment = new about(); break; case r.id.nav_menu3: fragment = new contact(); break; } //replacing fragment if (fragment != null) { fragmenttransaction ft = getsupportfragmentmanager().begintransaction(); ft.replace(r.id.content_frame, fragment); ft.commit(); } drawer.closedrawer(gravitycompat.start); } @suppresswarnings("statementwithemptybody") @override public boolean onnavigationitemselected(menuitem item) { //calling method displayselectedscreen , passing id of selected menu displayselectedscreen(item.getitemid()); //make method blank return true; } }
and main activity xml code
<android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:opendrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.navigationview android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitssystemwindows="true" app:headerlayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.drawerlayout>
so want add content such images , texts main activity xml page of app when add in main content of navigation looks , okay when click on example gallary fragment navigation drawer took me new page has content put in gallary xml problem content of main activity appear fragment pages click navigation drawer how can make content of each fragment appear without content of main activity understand me ? @_@
i suggest not use element other navigationview , fragment show screen navigationdrawer items, because if clicked on item redirected page later if ever wanted mainactivity page becomes impossible.either restart activity or try complex method achieve that.
instead should use fragments. can call default fragment loaded on opening app in onresume() method. fragment should available in navigation item later references.you can this:
fragment = new yourfragment(); transaction = getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.contentfragment,fragment).commitnow();
but if still wanted so, can set elements of mainactivity visibility gone. don't know if method work.
this should set on navigation item click
[your_element].setvisibility(view.gone);
Comments
Post a Comment