java - Open Android Navigation Drawer from an activity class -
i working on android navigation drawer
, through documentation looks like, drawer can extend fragment activity, open drawer activities, need make activities fragment, not feasible solution.
is there way can open drawer extends fragmentactivity activity?
when try extend drawer activity activity class, , activity open drawer extending draweractivity class (here slidemenuactivity), app crashes giving nullpointerexception.
below code opening drawer layout once first activity launches, unable access drawer.
app crashing on syncstate point in onpostcreate method
@override protected void onpostcreate(bundle savedinstancestate) { // todo auto-generated method stub super.onpostcreate(savedinstancestate); getactiondrawertoggle().syncstate(); } public class slidemenuactivity extends fragmentactivity implements onitemclicklistener { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); setdrawerlayout(); setdrawerlist(); if (savedinstancestate == null) { getdrawerlistview().setselectionafterheaderview(); getdrawerlistview().setselection(1); selectitem(1); } } // // @override // public void setcontentview(int layoutresid) { // // todo auto-generated method stub // super.setcontentview(layoutresid); // } private drawerlayout getdrawerview() { return (drawerlayout)findviewbyid(r.id.drawer_layout); } private listview getdrawerlistview() { return (listview) findviewbyid(r.id.left_drawer); } private actionbardrawertoggle getactiondrawertoggle() { actionbardrawertoggle drawertoggle=new actionbardrawertoggle( this, /* host activity */ getdrawerview(), /* 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 */ ) { @override public void ondrawerclosed(view view) { super.ondrawerclosed(view); } @override public void ondraweropened(view view) { super.ondraweropened(view); } }; return drawertoggle; } private void setdrawerlayout(){ // set custom shadow overlays main content when drawer opens getdrawerview().setdrawershadow(r.drawable.drawer_shadow, gravitycompat.relative_horizontal_gravity_mask); // enable actionbar app icon behave action toggle navigation drawer getdrawerview().setdrawerlistener(getactiondrawertoggle()); } /** * set drawer's list view items , click listener */ private void setdrawerlist() { imageview imageview=new imageview(this); imageview.setimagedrawable(getresources().getdrawable(r.drawable.precision_logo)); custombaseadapter adapter=new custombaseadapter(); adapter.list=getlistviewdata(); adapter.context=this; listview drawerlist=getdrawerlistview(); drawerlist.setheaderdividersenabled(true); drawerlist.addheaderview(imageview, null, false); drawerlist.setscrollingcacheenabled(false); drawerlist.setadapter(adapter); drawerlist.setonitemclicklistener(this); } private void selectitem(int position){ // update main content replacing fragments // fragment fragment=null; // fragmentmanager manager=getsupportfragmentmanager(); switch (position) { case 1: this.startactivity(new intent(this,savedtanklistactivity.class)); // fragment = new savedmixesfragment(); // fragment = new savedtanklistactivity(); // manager.begintransaction().replace(r.id.content_frame, fragment).commit(); break; case 2: // fragment=new mixguidefragment(); // manager.begintransaction().replace(r.id.content_frame, fragment).commit(); break; default: // fragment = new savedmixesfragment(); // manager.begintransaction().replace(r.id.content_frame, fragment).commit(); break; } getdrawerview().closedrawer(getdrawerlistview()); } @override protected void onpostcreate(bundle savedinstancestate) { // todo auto-generated method stub super.onpostcreate(savedinstancestate); getactiondrawertoggle().syncstate(); } @override public void onconfigurationchanged(configuration newconfig) { // todo auto-generated method stub super.onconfigurationchanged(newconfig); getactiondrawertoggle().onconfigurationchanged(newconfig); } private arraylist<drawerlistmodel> getlistviewdata() { arraylist<drawerlistmodel> listviewdata=new arraylist<drawerlistmodel>(); string[] listitemarray=getresources().getstringarray(r.array.slide_bar_list_item_array); for(int index=0;index<listitemarray.length;index++) { drawerlistmodel model=new drawerlistmodel(); model.listitem=listitemarray[index]; listviewdata.add(model); } return listviewdata; } @override public void onitemclick(adapterview<?> parent, view view, int position,long id) { view.setselected(true); selectitem(position); } public void opendrawerlist(view view){ getdrawerview().opendrawer(getdrawerlistview()); } }
i looking through documentation , thought had switch activities fragments. clear up, not case. can have many elements under linear layout or whatever needed.
for example if base activity without appdrawer is:
<relativelayout> <textview> </textview> <button> <button> </relativelayout>
simply do
<android.support.v4.widget.drawerlayout> <relativelayout> <textview> </textview> <button> <button> </relativelayout> <listview> </listview> </android.support.v4.widget.drawerlayout>
Comments
Post a Comment