android - Add section to navigation drawer -
i have below code :
activity.xml
<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" > <!-- framelayout display fragments --> <framelayout android:id="@+id/frame_container" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- listview display slider menu --> <listview android:id="@+id/list_slidermenu" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@color/list_background" android:choicemode="singlechoice" android:divider="@color/list_divider" android:dividerheight="1dp" android:listselector="@drawable/list_selector" /> </android.support.v4.widget.drawerlayout>
mainactivity.java
public class mainactivity extends activity { private drawerlayout mdrawerlayout; private listview mdrawerlist; private actionbardrawertoggle mdrawertoggle; // nav drawer title private charsequence mdrawertitle; // used store app title private charsequence mtitle; // slide menu items private string[] navmenutitles; private typedarray navmenuicons; private arraylist<navdraweritem> navdraweritems; private navdrawerlistadapter adapter; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mtitle = mdrawertitle = gettitle(); // load slide menu items navmenutitles = getresources().getstringarray(r.array.nav_drawer_items); // nav drawer icons resources navmenuicons = getresources() .obtaintypedarray(r.array.nav_drawer_icons); mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout); mdrawerlist = (listview) findviewbyid(r.id.list_slidermenu); navdraweritems = new arraylist<navdraweritem>(); // adding nav drawer items array // home navdraweritems.add(new navdraweritem(navmenutitles[0], navmenuicons .getresourceid(0, -1))); // find people navdraweritems.add(new navdraweritem(navmenutitles[1], navmenuicons .getresourceid(1, -1))); // photos navdraweritems.add(new navdraweritem(navmenutitles[2], navmenuicons .getresourceid(2, -1))); // communities, add counter here navdraweritems.add(new navdraweritem(navmenutitles[3], navmenuicons .getresourceid(3, -1), true, "22")); // pages navdraweritems.add(new navdraweritem(navmenutitles[4], navmenuicons .getresourceid(4, -1))); // what's hot, add counter here navdraweritems.add(new navdraweritem(navmenutitles[5], navmenuicons .getresourceid(5, -1), true, "50+")); // recycle typed array navmenuicons.recycle(); mdrawerlist.setonitemclicklistener(new slidemenuclicklistener()); // setting nav drawer list adapter adapter = new navdrawerlistadapter(getapplicationcontext(), navdraweritems); mdrawerlist.setadapter(adapter); // enabling action bar app icon , behaving toggle button getactionbar().setdisplayhomeasupenabled(true); getactionbar().sethomebuttonenabled(true); mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.drawable.ic_drawer, // nav menu toggle icon r.string.app_name, // nav drawer open - description // accessibility r.string.app_name // nav drawer close - description // accessibility ) { public void ondrawerclosed(view view) { getactionbar().settitle(mtitle); // calling onprepareoptionsmenu() show action bar icons invalidateoptionsmenu(); } public void ondraweropened(view drawerview) { getactionbar().settitle(mdrawertitle); // calling onprepareoptionsmenu() hide action bar icons invalidateoptionsmenu(); } }; mdrawerlayout.setdrawerlistener(mdrawertoggle); if (savedinstancestate == null) { // on first time display view first nav item displayview(0); } } /** * slide menu item click listener * */ private class slidemenuclicklistener implements listview.onitemclicklistener { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // display view selected nav drawer item displayview(position); } } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // toggle nav drawer on selecting action bar app icon/title if (mdrawertoggle.onoptionsitemselected(item)) { return true; } // handle action bar actions click switch (item.getitemid()) { case r.id.action_settings: return true; default: return super.onoptionsitemselected(item); } } /* * * called when invalidateoptionsmenu() triggered */ @override public boolean onprepareoptionsmenu(menu menu) { // if nav drawer opened, hide action items boolean draweropen = mdrawerlayout.isdraweropen(mdrawerlist); menu.finditem(r.id.action_settings).setvisible(!draweropen); return super.onprepareoptionsmenu(menu); } /** * diplaying fragment view selected nav drawer list item * */ private void displayview(int position) { // update main content replacing fragments fragment fragment = null; switch (position) { case 0: fragment = new homefragment(); break; case 1: fragment = new findpeoplefragment(); break; case 2: fragment = new photosfragment(); break; case 3: fragment = new communityfragment(); break; case 4: fragment = new pagesfragment(); break; case 5: fragment = new whatshotfragment(); break; default: break; } if (fragment != null) { fragmentmanager fragmentmanager = getfragmentmanager(); fragmentmanager.begintransaction() .replace(r.id.frame_container, fragment).commit(); // update selected item , title, close drawer mdrawerlist.setitemchecked(position, true); mdrawerlist.setselection(position); settitle(navmenutitles[position]); mdrawerlayout.closedrawer(mdrawerlist); } else { // error in creating fragment log.e("mainactivity", "error in creating fragment"); } } @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); } }
and section have below xml :
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#fff" > <textview android:id="@+id/navmenusection_label" style="?android:attr/listseparatortextviewstyle" android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#bbb" android:paddingbottom="7dp" android:paddingtop="7dp" android:text="section" android:textcolor="#ffffff" android:textsize="18sp" /> </relativelayout>
so how can add section in navigation drawer. ? have change navdrawerlistadapter.java file :
public class navdrawerlistadapter extends baseadapter { private context context; private arraylist<navdraweritem> navdraweritems; public navdrawerlistadapter(context context, arraylist<navdraweritem> navdraweritems) { this.context = context; this.navdraweritems = navdraweritems; } @override public int getcount() { return navdraweritems.size(); } @override public object getitem(int position) { return navdraweritems.get(position); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (convertview == null) { layoutinflater minflater = (layoutinflater) context .getsystemservice(activity.layout_inflater_service); convertview = minflater.inflate(r.layout.drawer_list_item, null); /* * convertview = minflater.inflate(r.layout.navdrawer_section, * parent, false); */ } imageview imgicon = (imageview) convertview.findviewbyid(r.id.icon); textview txttitle = (textview) convertview.findviewbyid(r.id.title); textview txtcount = (textview) convertview.findviewbyid(r.id.counter); imgicon.setimageresource(navdraweritems.get(position).geticon()); txttitle.settext(navdraweritems.get(position).gettitle()); // displaying count // check whether set visible or not if (navdraweritems.get(position).getcountervisibility()) { txtcount.settext(navdraweritems.get(position).getcount()); } else { // hide counter view txtcount.setvisibility(view.gone); } return convertview; } }
convertview = minflater.inflate(r.layout.navdrawer_section, parent, false);
i have put in getview()
getting error .
logcat :
fatal exception: main process: com.novumlogic.euphoria, pid: 26846 java.lang.nullpointerexception @ com.novumlogic.euphoria.adapter.navdrawerlistadapter.getview(navdrawerlistadapter.java:59) @ android.widget.abslistview.obtainview(abslistview.java:2338) @ android.widget.listview.makeandaddview(listview.java:1812) @ android.widget.listview.filldown(listview.java:698) @ android.widget.listview.fillfromtop(listview.java:759) @ android.widget.listview.layoutchildren(listview.java:1631) @ android.widget.abslistview.onlayout(abslistview.java:2149) @ android.view.view.layout(view.java:15125) @ android.view.viewgroup.layout(viewgroup.java:4862) @ android.support.v4.widget.drawerlayout.onlayout(drawerlayout.java:702) @ android.view.view.layout(view.java:15125) @ android.view.viewgroup.layout(viewgroup.java:4862) @ android.widget.framelayout.layoutchildren(framelayout.java:515) @ android.widget.framelayout.onlayout(framelayout.java:450) @ android.view.view.layout(view.java:15125) @ android.view.viewgroup.layout(viewgroup.java:4862) @ com.android.internal.widget.actionbaroverlaylayout.onlayout(actionbaroverlaylayout.java:374) @ android.view.view.layout(view.java:15125) @ android.view.viewgroup.layout(viewgroup.java:4862) @ android.widget.framelayout.layoutchildren(framelayout.java:515) @ android.widget.framelayout.onlayout(framelayout.java:450) @ android.view.view.layout(view.java:15125) @ android.view.viewgroup.layout(viewgroup.java:4862) @ android.view.viewrootimpl.performlayout(viewrootimpl.java:2317) @ android.view.viewrootimpl.performtraversals(viewrootimpl.java:2023) @ android.view.viewrootimpl.dotraversal(viewrootimpl.java:1189) @ android.view.viewrootimpl$traversalrunnable.run(viewrootimpl.java:6223) @ android.view.choreographer$callbackrecord.run(choreographer.java:788) @ android.view.choreographer.docallbacks(choreographer.java:591) @ android.view.choreographer.doframe(choreographer.java:560) @ android.view.choreographer$framedisplayeventreceiver.run(choreographer.java:774) @ android.os.handler.handlecallback(handler.java:808) @ android.os.handler.dispatchmessage(handler.java:103) @ android.os.looper.loop(looper.java:193) @ android.app.activitythread.main(activitythread.java:5333) @ java.lang.reflect.method.invokenative(native method) @ java.lang.reflect.method.invoke(method.java:515) @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:824) @ com.android.internal.os.zygoteinit.main(zygoteinit.java:640) @ dalvik.system.nativestart.main(native method)
how can add section ?
first remove changes have done causes null pointer exception go drwer_list_item.xml
, add text view section part, go navdraweritem.class
, add:
private string section;
change constructor this:
public navdraweritem(string title, int icon,string section){ this.title = title; this.icon = icon; this.section= section; }
you have add getters , setters private section instanse:
public string getsection(){ return this.section; } public void setsection(string section){ this.section = section; }
then go navdrewerlistadapter.class
, add this:
textview txtsection = (textview) convertview.findviewbyid(r.id.section);
last thing have change navdraweritems.add
this:
navdraweritems.add(new navdraweritem(navmenutitles[index], navmenuicons .getresourceid(index, -1)));
to this:
navdraweritems.add(new navdraweritem(navmenutitles[index], navmenuicons .getresourceid(index, -1),"string section"));
hope help.
Comments
Post a Comment