android - Navigation Drawer not working -
i combined navigation drawer tabs layout. in tab, layout working 3 tabs navigation drawer misbehaving. navigation drawer icon disappeared, opening sliding not getting closed sliding. , menus in navigation drawer not working.
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayoutxmlns: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" /> <linearlayout android:id="@+id/main_layout" android:orientation="vertical" 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:layout_width="match_parent" android:layout_height="match_parent" tools:context=".mainactivity"> <!-- our toolbar --> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorprimary" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar" app:popuptheme="@style/themeoverlay.appcompat.light"/> <!-- our tablayout display tabs --> <android.support.design.widget.tablayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorprimary" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar"/> <!-- view pager swipe views --> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent"/> </linearlayout> public class mainactivity extends appcompatactivity implements tablayout.ontabselectedlistener { private navigationview navigationview; private drawerlayout drawer; private view navheader; private imageview imgnavheaderbg, imgprofile; private textview txtname, txtwebsite; private toolbar toolbar; private floatingactionbutton fab; actionbar actionbar; //this our tablayout private tablayout tablayout; //this our viewpager private viewpager viewpager; // urls load navigation header background image // , profile image private static final string urlnavheaderbg = "http://api.androidhive.info/images/nav-menu-header-bg.jpg"; private static final string urlprofileimg = "https://lh3.googleusercontent.com/ecte_g34m9ygdkmopyvcag1vbarcmzwnvs6rs5t4jlzj6qgqsbqum0nutscplhybkljoys-txg"; // index identify current nav menu item public static int navitemindex = 0; // tags used attach fragments private static final string tag_home = "home"; private static final string tag_photos = "photos"; private static final string tag_movies = "movies"; private static final string tag_notifications = "notifications"; private static final string tag_settings = "settings"; public static string current_tag = tag_home; // toolbar titles respected selected nav menu item private string[] activitytitles; // flag load home fragment when user presses key private boolean shouldloadhomefragonbackpress = true; private handler mhandler; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); actionbar = getsupportactionbar(); actionbar.show(); toolbar = (toolbar) findviewbyid(r.id.toolbar); // setsupportactionbar(toolbar); mhandler = new handler(); drawer = (drawerlayout) findviewbyid(r.id.drawer_layout); navigationview = (navigationview) findviewbyid(r.id.nav_view); view hview = navigationview.getheaderview(0); // fab = (floatingactionbutton) findviewbyid(r.id.fab); // navigation view header navheader = navigationview.getheaderview(0); txtname = (textview) navheader.findviewbyid(r.id.name); txtwebsite = (textview) navheader.findviewbyid(r.id.website); imgnavheaderbg = (imageview) navheader.findviewbyid(r.id.img_header_bg); imgprofile = (imageview) navheader.findviewbyid(r.id.img_profile); // load toolbar titles string resources activitytitles = getresources().getstringarray(r.array.nav_item_activity_titles); /* fab.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { snackbar.make(view, "replace own action", snackbar.length_long) .setaction("action", null).show(); } });*/ // load nav menu header data loadnavheader(); // initializing navigation menu setupnavigationview(); if (savedinstancestate == null) { navitemindex = 0; current_tag = tag_home; loadhomefragment(); } /* toolbar toolbar = (toolbar) findviewbyid(r.id.toolbar); setsupportactionbar(toolbar);*/ //initializing tablayout tablayout = (tablayout) findviewbyid(r.id.tablayout); //adding tabs using addtab() method tablayout.addtab(tablayout.newtab().settext("home")); tablayout.addtab(tablayout.newtab().settext("search bride")); tablayout.addtab(tablayout.newtab().settext("search groom")); tablayout.settabgravity(tablayout.gravity_fill); //initializing viewpager viewpager = (viewpager) findviewbyid(r.id.pager); //creating our pager adapter pager adapter = new pager(getsupportfragmentmanager(), tablayout.gettabcount()); //adding adapter pager viewpager.setadapter(adapter); tablayout.setontabselectedlistener(this); viewpager.addonpagechangelistener(new viewpager.onpagechangelistener() { @override public void onpagescrolled(int position, float positionoffset, int positionoffsetpixels) { tablayout.setscrollposition(position, positionoffset, true); } @override public void onpageselected(int position) { } @override public void onpagescrollstatechanged(int state) { } //adding ontabselectedlistener swipe views //tablayout.setonclicklistener(); }); } /*** * load navigation menu header information * background image, profile image * name, website, notifications action view (dot) */ private void loadnavheader() { // name, website txtname.settext("shubham"); txtwebsite.settext("shubhamkanugo@gmail.com"); // loading header background image glide.with(this).load(urlnavheaderbg) .crossfade() .diskcachestrategy(diskcachestrategy.all) .into(imgnavheaderbg); // loading profile image glide.with(this).load(urlprofileimg) .crossfade() .thumbnail(0.5f) .bitmaptransform(new circletransform(this)) .diskcachestrategy(diskcachestrategy.all) .into(imgprofile); // showing dot next notifications label navigationview.getmenu().getitem(3).setactionview(r.layout.menu_dot); } /*** * returns respected fragment user * selected navigation menu */ private void loadhomefragment() { // selecting appropriate nav menu item selectnavmenu(); // set toolbar title settoolbartitle(); // if user select current navigation menu again, don't // close navigation drawer if (getsupportfragmentmanager().findfragmentbytag(current_tag) != null) { drawer.closedrawers(); // show or hide fab button //togglefab(); return; } // sometimes, when fragment has huge data, screen seems hanging // when switching between navigation menus // using runnable, fragment loaded cross fade effect // effect can seen in gmail app runnable mpendingrunnable = new runnable() { @override public void run() { // update main content replacing fragments homefragment fragment = gethomefragment(); fragmenttransaction fragmenttransaction = getsupportfragmentmanager().begintransaction(); fragmenttransaction.setcustomanimations(android.r.anim.fade_in, android.r.anim.fade_out); // fragmenttransaction replace = fragmenttransaction.replace(r.id.frame, fragment, current_tag); fragmenttransaction.commitallowingstateloss(); } }; // if mpendingrunnable not null, add message queue if (mpendingrunnable != null) { mhandler.post(mpendingrunnable); } // show or hide fab button // togglefab(); //closing drawer on item click drawer.closedrawers(); // refresh toolbar menu invalidateoptionsmenu(); } private homefragment gethomefragment() { switch (navitemindex) { case 0: // home homefragment homefragment = new homefragment(); return homefragment; case 1: // photos photosfragment photosfragment = new photosfragment(); return photosfragment; case 2: // movies fragment moviesfragment moviesfragment = new moviesfragment(); return moviesfragment; case 3: // notifications fragment notificationfragment notificationsfragment = new notificationfragment(); return notificationsfragment; case 4: // settings fragment settingsfragment settingsfragment = new settingsfragment(); return settingsfragment; default: return new homefragment(); } } private void settoolbartitle() { getsupportactionbar().settitle(activitytitles[navitemindex]); } private void selectnavmenu() { navigationview.getmenu().getitem(navitemindex).setchecked(true); } private void setupnavigationview() { //setting navigation view item selected listener handle item click of navigation menu navigationview.setnavigationitemselectedlistener(new navigationview.onnavigationitemselectedlistener() { // method trigger on item click of navigation menu @override public boolean onnavigationitemselected(menuitem menuitem) { //check see item being clicked , perform appropriate action switch (menuitem.getitemid()) { //replacing main content contentfragment our inbox view; case r.id.nav_home: navitemindex = 0; current_tag = tag_home; break; case r.id.nav_photos: navitemindex = 1; current_tag = tag_photos; break; case r.id.nav_movies: navitemindex = 2; current_tag = tag_movies; break; case r.id.nav_notifications: navitemindex = 3; current_tag = tag_notifications; break; case r.id.nav_settings: navitemindex = 4; current_tag = tag_settings; break; case r.id.nav_about_us: // launch new intent instead of loading fragment startactivity(new intent(mainactivity.this, aboutusactivity.class)); drawer.closedrawers(); return true; case r.id.nav_privacy_policy: // launch new intent instead of loading fragment startactivity(new intent(mainactivity.this, privacypolicyactivity.class)); drawer.closedrawers(); return true; default: navitemindex = 0; } //checking if item in checked state or not, if not make in checked state if (menuitem.ischecked()) { menuitem.setchecked(false); } else { menuitem.setchecked(true); } menuitem.setchecked(true); loadhomefragment(); return true; } }); actionbardrawertoggle actionbardrawertoggle = new actionbardrawertoggle(this, drawer,toolbar, r.string.opendrawer, r.string.closedrawer) { @override public void ondrawerclosed(view drawerview) { // code here triggered once drawer closes dont want happen leave blank super.ondrawerclosed(drawerview); } @override public void ondraweropened(view drawerview) { // code here triggered once drawer open dont want happen leave blank super.ondraweropened(drawerview); } }; //setting actionbartoggle drawer layout drawer.setdrawerlistener(actionbardrawertoggle); //calling sync state necessary or else hamburger icon wont show actionbardrawertoggle.syncstate(); } @override public void onbackpressed() { if (drawer.isdraweropen(gravitycompat.start)) { drawer.closedrawers(); return; } // code loads home fragment when key pressed // when user in other fragment home if (shouldloadhomefragonbackpress) { // checking if user on other navigation menu // rather home if (navitemindex != 0) { navitemindex = 0; current_tag = tag_home; loadhomefragment(); return; } } super.onbackpressed(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. // show menu when home fragment selected if (navitemindex == 0) { getmenuinflater().inflate(r.menu.main, menu); } // when fragment notifications, load menu created notifications if (navitemindex == 3) { getmenuinflater().inflate(r.menu.notifications, menu); } return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); //noinspection simplifiableifstatement if (id == r.id.action_logout) { toast.maketext(getapplicationcontext(), "logout user!", toast.length_long).show(); return true; } // user in notifications fragment // , selected 'mark read' if (id == r.id.action_mark_all_read) { toast.maketext(getapplicationcontext(), "all notifications marked read!", toast.length_long).show(); } // user in notifications fragment // , selected 'clear all' if (id == r.id.action_clear_notifications) { toast.maketext(getapplicationcontext(), "clear notifications!", toast.length_long).show(); } return super.onoptionsitemselected(item); } // show or hide fab @override public void ontabselected(tablayout.tab tab) { viewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(tablayout.tab tab) { } @override public void ontabreselected(tablayout.tab tab) { }
hi please follow order tabs navigation drawer
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" tools:opendrawer="start"> <android.support.design.widget.coordinatorlayout 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/main_content" android:layout_width="match_parent" android:layout_height="match_parent" android:fitssystemwindows="true" android:background="#ffffff"> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent"> <android.support.design.widget.appbarlayout android:id="@+id/appbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:paddingtop="@dimen/appbar_padding_top" android:theme="@style/maintheme.appbaroverlay"> <android.support.v7.widget.toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionbarsize" android:background="?attr/colorprimary" app:layout_scrollflags="scroll|enteralways" app:popuptheme="@style/maintheme.popupoverlay"/> <android.support.design.widget.tablayout android:id="@+id/tablayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="?attr/colorprimary" android:minheight="?attr/actionbarsize" android:theme="@style/themeoverlay.appcompat.dark.actionbar"/> </android.support.design.widget.appbarlayout> <!-- view pager swipe views --> <android.support.v4.view.viewpager android:id="@+id/pager" android:layout_width="match_parent" android:layout_height="fill_parent"/> </relativelayout> <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>
Comments
Post a Comment