android - How to place NavigationDrawer behind layout? -
i sorry english using navigation drawer , want navigation drawer behind layout in facebook. here code, more want drawer listview under framelayout. please. thanks.
public class mainactivity extends activity { private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; private string[] mplanettitles; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtitle = mdrawertitle = gettitle(); mplanettitles = getresources().getstringarray(r.array.planets_array); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); // set custom shadow overlays main content when drawer opens mdrawerlayout.setdrawershadow(r.drawable.drawer_shadow, gravitycompat.start); // set drawer's list view items , click listener mdrawerlist.setadapter(new arrayadapter<string>(this, r.layout.drawer_list_item, mplanettitles)); mdrawerlist.setonitemclicklistener(new draweritemclicklistener()); // enable actionbar app icon behave action toggle nav drawer getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); // actionbardrawertoggle ties the proper interactions // between sliding drawer , action bar app icon mdrawertoggle = new actionbardrawertoggle( this, /* host activity */ mdrawerlayout, /* drawerlayout object */ r.drawable.ic_drawer, /* nav drawer image replace 'up' caret */ r.string.drawer_open, /* "open drawer" description accessibility */ r.string.drawer_close /* "close drawer" description accessibility */ ) { public void ondrawerclosed(view view) { getactionbar().settitle(mtitle); invalidateoptionsmenu(); // creates call onprepareoptionsmenu() } public void ondraweropened(view drawerview) { getactionbar().settitle(mdrawertitle); invalidateoptionsmenu(); // creates call onprepareoptionsmenu() } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (savedinstancestate == null) { selectitem(0); } } @override public boolean oncreateoptionsmenu(menu menu) { menuinflater inflater = getmenuinflater(); inflater.inflate(r.menu.main, menu); return super.oncreateoptionsmenu(menu); } /* called whenever call invalidateoptionsmenu() */ @override public boolean onprepareoptionsmenu(menu menu) { // if nav drawer open, hide action items related content view boolean draweropen = mdrawerlayout.isdraweropen(mdrawerlist); menu.finditem(r.id.action_websearch).setvisible(!draweropen); return super.onprepareoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { // action bar home/up action should open or close drawer. // actionbardrawertoggle take care of this. if (mdrawertoggle.onoptionsitemselected(item)) { return true; } // handle action buttons switch(item.getitemid()) { case r.id.action_websearch: // create intent perform web search planet intent intent = new intent(intent.action_web_search); intent.putextra(searchmanager.query, getactionbar().gettitle()); // catch event there's no activity handle intent if (intent.resolveactivity(getpackagemanager()) != null) { startactivity(intent); } else { toast.maketext(this, r.string.app_not_available, toast.length_long).show(); } return true; default: return super.onoptionsitemselected(item); } } /* click listner listview in navigation drawer */ private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitem(position); } } private void selectitem(int position) { // update main content replacing fragments fragment fragment = new planetfragment(); bundle args = new bundle(); args.putint(planetfragment.arg_planet_number, position); fragment.setarguments(args); fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction().replace(r.id.content_frame, fragment).commit(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); settitle(mplanettitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } @override public void settitle(charsequence title) { mtitle = title; getactionbar().settitle(mtitle); } /** * when using actionbardrawertoggle, must call during * onpostcreate() , onconfigurationchanged()... */ @override protected void onpostcreate(bundle savedinstancestate) { super.onpostcreate(savedinstancestate); // sync toggle state after onrestoreinstancestate has occurred. mdrawertoggle.syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { super.onconfigurationchanged(newconfig); // pass configuration change drawer toggls mdrawertoggle.onconfigurationchanged(newconfig); } /** * fragment appears in "content_frame", shows planet */ public static class planetfragment extends fragment { public static final string arg_planet_number = "planet_number"; public planetfragment() { // empty constructor required fragment subclasses } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_planet, container, false); int = getarguments().getint(arg_planet_number); string planet = getresources().getstringarray(r.array.planets_array)[i]; int imageid = getresources().getidentifier(planet.tolowercase(locale.getdefault()), "drawable", getactivity().getpackagename()); ((imageview) rootview.findviewbyid(r.id.image)).setimageresource(imageid); getactivity().settitle(planet); return rootview; } } } <android.support.v4.widget.drawerlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent"> <!-- main content view, view below consumes entire space available using match_parent in both dimensions. --> <framelayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- android:layout_gravity="start" tells drawerlayout treat sliding drawer on left side left-to-right languages , on right side right-to-left languages. drawer given fixed width in dp , extends full height of container. solid background used contrast content view. --> <listview android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:choicemode="singlechoice" android:divider="@android:color/transparent" android:dividerheight="0dp" android:background="#111"/> </android.support.v4.widget.drawerlayout>
use contentdisplacedrawertoggle
library, if want combine actionbardrawertoggle, might want use wrapper provided in library.
malltoggles = new actionbardrawertogglewrapper(this, mdrawerlayout, r.drawable.ic_drawer, r.string.drawer_open, r.string.drawer_close); malltoggles.addpartnertoggle(new contentdisplacedrawertoggle(this, mdrawerlayout, r.id.content_frame)); malltoggles.setdrawerindicatorenabled(true); mdrawerlayout.setdrawerlistener(malltoggles);
adding shadow (along left margin) content view instead drawer make drawer behind content view.
Comments
Post a Comment