android - NavigationDrawer main page and styling -
this first application creating using navigationdrawer
. have pretty simple question. how make first page in navigationdrawer
main one? i'm not familiar formatting since first time using drawer appreciate if more familiar tell me if doing correctly. right each page displays text more. , 1 of questions how make clicking page in drawer can open new page using relativelayout
example. understanding adapters views, create new activity , call startactivity()
in iteration drawerclick? if so, efficient? meaning take long time page load? main activity is:
public class mainactivity extends activity { private string[] mpages; private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; private charsequence mdrawertitle; private charsequence mtitle; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.drawer_layout); mpages = getresources().getstringarray(r.array.page_titles); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.left_drawer); mdrawerlist.setadapter(new arrayadapter<string>(this, r.layout.drawer_list_item, mpages)); mtitle = mdrawertitle = gettitle(); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.ic_drawer, r.string.drawer_open, r.string.drawer_close) { /** called when drawer has settled in closed state. */ public void ondrawerclosed(view view) { getactionbar().settitle(mtitle); } /** called when drawer has settled in open state. */ public void ondraweropened(view drawerview) { getactionbar().settitle(mdrawertitle); } }; // set drawer toggle drawerlistener mdrawerlayout.setdrawerlistener(mdrawertoggle); mdrawerlist.setonitemclicklistener(new draweritemclicklistener()); getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); } @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); mdrawertoggle.onconfigurationchanged(newconfig); } @override public boolean onoptionsitemselected(menuitem item) { // pass event actionbardrawertoggle, if returns // true, has handled app icon touch event if (mdrawertoggle.onoptionsitemselected(item)) { return true; } // handle other action bar items... return super.onoptionsitemselected(item); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } private class draweritemclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitem(position); } } /** swaps fragments in main content view */ private void selectitem(int position) { // create new fragment , specify planet show based on position fragment fragment; if(position == 0){ fragment = new onefragment(); // insert fragment replacing existing fragment android.app.fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.content_frame, fragment) .commit(); } else if(position == 1){ fragment = new twofragment(); // insert fragment replacing existing fragment android.app.fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.content_frame, fragment) .commit(); } else if(position == 2){ fragment = new threefragment(); // insert fragment replacing existing fragment android.app.fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.content_frame, fragment) .commit(); } else if(position == 3){ fragment = new fourfragment(); // insert fragment replacing existing fragment android.app.fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.content_frame, fragment) .commit(); } // highlight selected item, update title, , close drawer mdrawerlist.setitemchecked(position, true); settitle(mpages[position]); mdrawerlayout.closedrawer(mdrawerlist); } @override public void settitle(charsequence title) { mtitle = title; getactionbar().settitle(mtitle); } public static class onefragment extends fragment{ public onefragment(){ } view rootview; textview text; @override public view oncreateview(layoutinflater inflater, viewgroup contatiner, bundle savedinstancestate){ rootview = inflater.inflate(r.layout.drawer_layout, contatiner, false); text = (textview)rootview.findviewbyid(r.id.text_view1); text.settext("one"); return rootview; } } public static class twofragment extends fragment{ public twofragment(){ } view rootview; textview text; @override public view oncreateview(layoutinflater inflater, viewgroup contatiner, bundle savedinstancestate){ rootview = inflater.inflate(r.layout.drawer_layout, contatiner, false); text = (textview)rootview.findviewbyid(r.id.text_view1); text.settext("two"); return rootview; } } public static class threefragment extends fragment{ public threefragment(){ } view rootview; textview text; @override public view oncreateview(layoutinflater inflater, viewgroup contatiner, bundle savedinstancestate){ rootview = inflater.inflate(r.layout.drawer_layout, contatiner, false); text = (textview)rootview.findviewbyid(r.id.text_view1); text.settext("three"); return rootview; } } public static class fourfragment extends fragment{ public fourfragment(){ } view rootview; textview text; @override public view oncreateview(layoutinflater inflater, viewgroup contatiner, bundle savedinstancestate){ rootview = inflater.inflate(r.layout.drawer_layout, contatiner, false); text = (textview)rootview.findviewbyid(r.id.text_view1); text.settext("four"); return rootview; } } }
i apologize lengthy question, developer site wasn't helping out , want make sure correctly first time don't have go much
whatever want show needs within child of drawerlayout. in case, think fragment transaction in oncreate()
put whatever fragment want visible first inside of content area.
if (savedinstancestate == null) { fragment = new onefragment(); // insert fragment replacing existing fragment getfragmentmanager().begintransaction() .replace(r.id.content_frame, fragment).commit(); }
you can hold references these fragments don't create new instance of them each time user makes selection. want make sure new selection isn't same current one, or else have transactions don't need.
for opening "new page", want use startactivity()
, show activity it's own layout. speaking, don't concerned how long takes activity load unless doing meaningful work (like loading bunch of data out of database).
lastly, adapter
s adapterview
s (like listview
) , entirely different matter. used in conjunction specific ui components generate child views representing potentially large data sets , can recycled efficiency reasons. suggest watch the world of listview if want more information/clarity that.
Comments
Post a Comment