android - How add list select item in navigationview -


how can add list items in navigationview, need 1 list items hiden in navigationview user can clik in item when click, list show items below item clicked, how next example:

enter image description here

you can in this example. added code here have full example.

first of need add custom adapter:

public class customexpandablelistadapter extends baseexpandablelistadapter {      private context mcontext;     private list<string> mexpandablelisttitle;     private map<string, list<string>> mexpandablelistdetail;     private layoutinflater mlayoutinflater;      public customexpandablelistadapter(context context, list<string> expandablelisttitle,                                        map<string, list<string>> expandablelistdetail) {         mcontext = context;         mexpandablelisttitle = expandablelisttitle;         mexpandablelistdetail = expandablelistdetail;         mlayoutinflater = (layoutinflater) mcontext.getsystemservice(context.layout_inflater_service);     }      @override     public object getchild(int listposition, int expandedlistposition) {         return mexpandablelistdetail.get(mexpandablelisttitle.get(listposition))             .get(expandedlistposition);     }      @override     public long getchildid(int listposition, int expandedlistposition) {         return expandedlistposition;     }      @override     public view getchildview(int listposition, final int expandedlistposition,                              boolean islastchild, view convertview, viewgroup parent) {         final string expandedlisttext = (string) getchild(listposition, expandedlistposition);         if (convertview == null) {             convertview = mlayoutinflater.inflate(r.layout.list_item, null);         }         textview expandedlisttextview = (textview) convertview             .findviewbyid(r.id.expandedlistitem);         expandedlisttextview.settext(expandedlisttext);         return convertview;     }      @override     public int getchildrencount(int listposition) {         return mexpandablelistdetail.get(mexpandablelisttitle.get(listposition))             .size();     }      @override     public object getgroup(int listposition) {         return mexpandablelisttitle.get(listposition);     }      @override     public int getgroupcount() {         return mexpandablelisttitle.size();     }      @override     public long getgroupid(int listposition) {         return listposition;     }      @override     public view getgroupview(int listposition, boolean isexpanded,                              view convertview, viewgroup parent) {         string listtitle = (string) getgroup(listposition);         if (convertview == null) {             convertview = mlayoutinflater.inflate(r.layout.list_group, null);         }         textview listtitletextview = (textview) convertview             .findviewbyid(r.id.listtitle);         listtitletextview.settypeface(null, typeface.bold);         listtitletextview.settext(listtitle);         return convertview;     }      @override     public boolean hasstableids() {         return false;     }      @override     public boolean ischildselectable(int listposition, int expandedlistposition) {         return true;     } } 

then can add source list:

public class expandablelistdatasource {      /**      * returns fake data of films      *      * @param context      * @return      */     public static map<string, list<string>> getdata(context context) {         map<string, list<string>> expandablelistdata = new treemap<>();          list<string> filmgenres = arrays.aslist(context.getresources().getstringarray(r.array.film_genre));          list<string> actionfilms = arrays.aslist(context.getresources().getstringarray(r.array.actionfilms));         list<string> musicalfilms = arrays.aslist(context.getresources().getstringarray(r.array.musicals));         list<string> dramafilms = arrays.aslist(context.getresources().getstringarray(r.array.dramas));         list<string> thrillerfilms = arrays.aslist(context.getresources().getstringarray(r.array.thrillers));         list<string> comedyfilms = arrays.aslist(context.getresources().getstringarray(r.array.comedies));          expandablelistdata.put(filmgenres.get(0), actionfilms);         expandablelistdata.put(filmgenres.get(1), musicalfilms);         expandablelistdata.put(filmgenres.get(2), dramafilms);         expandablelistdata.put(filmgenres.get(3), thrillerfilms);         expandablelistdata.put(filmgenres.get(4), comedyfilms);          return expandablelistdata;     } } 

it takes data string.xml:

<resources>     <string name="app_name">expandablenavigationdrawer</string>     <string name="action_settings">settings</string>     <string name="drawer_open">open navigation drawer</string>     <string name="drawer_close">close navigation drawer</string>     <string name="film_genres">film genres</string>     <string name="website">https://en.wikipedia.org/wiki/film</string>     <string name="date">22 oct 2015</string>     <string name="selected_item">selected item</string>      <string-array name="film_genre">         <item>action</item>         <item>musical</item>         <item>drama</item>         <item>thriller</item>         <item>comedy</item>     </string-array>      <string-array name="actionfilms">         <item>dr. no (1962)</item>         <item>goldfinger (1964)</item>         <item>thunderball (1965)</item>         <item>live , let die (1973)</item>         <item>moonraker (1979)</item>         <item>for eyes (1981)</item>         <item>octopussy (1983)</item>         <item>a view kill (1985)</item>         <item>licence kill (1989)</item>         <item>goldeneye (1995)</item>     </string-array>      <string-array name="musicals">         <item>naughty marietta (1935)</item>         <item>rose marie (1936)</item>         <item>maytime (1937)</item>         <item>sweethearts (1938)</item>         <item>bitter sweet (1940)</item>         <item>new moon (1940)</item>         <item>i married angel (1942)</item>         <item>lady (1941)</item>         <item>ship ahoy (1942)</item>         <item>sensations of 1945 (1944)</item>     </string-array>      <string-array name="dramas">         <item>home of brave (1949)</item>         <item>the accused (1949)</item>         <item>12 angry men (1957)</item>         <item>compulsion (1959)</item>         <item>inherit wind (1960)</item>         <item>to kill mockingbird (1962)</item>         <item>mrs. miniver (1942)</item>         <item>since went away (1944)</item>         <item>the champ (1931)</item>         <item>nashville (1975)</item>     </string-array>      <string-array name="thrillers">         <item>alien (1979)</item>         <item>the french connection (1971)</item>         <item>high noon (1952)</item>         <item>double indemnity (1944)</item>         <item>safety last (1923)</item>         <item>the lady shanghai (1948)</item>         <item>the third man (1949)</item>         <item>rear window (1954)</item>         <item>the 39 steps (1935)</item>         <item>shadow of doubt (1943)</item>     </string-array>      <string-array name="comedies">         <item>safety last (1923)</item>         <item>duck soup (1933)</item>         <item>cat ballou (1965)</item>         <item>what\'s up, tiger lily? (1966)</item>         <item>blazing saddles (1974)</item>         <item>play again, sam (1972)</item>         <item>the cheap detective (1978)</item>         <item>the naked gun (1988)</item>         <item>the freshman (1990)</item>         <item>waiting guffman (1996) </item>     </string-array>  </resources> 

and can add mainactivity:

public class mainactivity extends actionbaractivity {      private drawerlayout mdrawerlayout;     private actionbardrawertoggle mdrawertoggle;     private string mactivitytitle;      private expandablelistview mexpandablelistview;     private expandablelistadapter mexpandablelistadapter;     private list<string> mexpandablelisttitle;     private map<string, list<string>> mexpandablelistdata;     private textview mselecteditemview;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_main);          mdrawerlayout = (drawerlayout) findviewbyid(r.id.drawer_layout);         mactivitytitle = gettitle().tostring();          mexpandablelistview = (expandablelistview) findviewbyid(r.id.navlist);         mselecteditemview = (textview) findviewbyid(r.id.selected_item);          layoutinflater inflater = getlayoutinflater();         view listheaderview = inflater.inflate(r.layout.nav_header, null, false);         mexpandablelistview.addheaderview(listheaderview);          mexpandablelistdata = expandablelistdatasource.getdata(this);         mexpandablelisttitle = new arraylist(mexpandablelistdata.keyset());          adddraweritems();         setupdrawer();          getsupportactionbar().setdisplayhomeasupenabled(true);         getsupportactionbar().sethomebuttonenabled(true);     }      private void adddraweritems() {         mexpandablelistadapter = new customexpandablelistadapter(this, mexpandablelisttitle, mexpandablelistdata);         mexpandablelistview.setadapter(mexpandablelistadapter);         mexpandablelistview.setongroupexpandlistener(new expandablelistview.ongroupexpandlistener() {             @override             public void ongroupexpand(int groupposition) {                 getsupportactionbar().settitle(mexpandablelisttitle.get(groupposition).tostring());                 mselecteditemview.settext(mexpandablelisttitle.get(groupposition).tostring());             }         });          mexpandablelistview.setongroupcollapselistener(new expandablelistview.ongroupcollapselistener() {             @override             public void ongroupcollapse(int groupposition) {                 getsupportactionbar().settitle(r.string.film_genres);                 mselecteditemview.settext(r.string.selected_item);             }         });          mexpandablelistview.setonchildclicklistener(new expandablelistview.onchildclicklistener() {             @override             public boolean onchildclick(expandablelistview parent, view v,                                         int groupposition, int childposition, long id) {                 string selecteditem = ((list) (mexpandablelistdata.get(mexpandablelisttitle.get(groupposition))))                     .get(childposition).tostring();                 getsupportactionbar().settitle(selecteditem);                 mselecteditemview.settext(mexpandablelisttitle.get(groupposition).tostring() + " -> " + selecteditem);                 mdrawerlayout.closedrawer(gravitycompat.start);                 return false;             }         });     }      private void setupdrawer() {         mdrawertoggle = new actionbardrawertoggle(this, mdrawerlayout, r.string.drawer_open, r.string.drawer_close) {              /** called when drawer has settled in open state. */             public void ondraweropened(view drawerview) {                 super.ondraweropened(drawerview);                 getsupportactionbar().settitle(r.string.film_genres);                 invalidateoptionsmenu(); // creates call onprepareoptionsmenu()             }              /** called when drawer has settled in closed state. */             public void ondrawerclosed(view view) {                 super.ondrawerclosed(view);                 getsupportactionbar().settitle(mactivitytitle);                 invalidateoptionsmenu(); // creates call onprepareoptionsmenu()             }         };          mdrawertoggle.setdrawerindicatorenabled(true);         mdrawerlayout.setdrawerlistener(mdrawertoggle);     }      @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 oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_main, 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_settings) {             return true;         }          // activate navigation drawer toggle         if (mdrawertoggle.onoptionsitemselected(item)) {             return true;         }          return super.onoptionsitemselected(item);     } } 

you can try different approach: using 3rd party library called material drawer. in documentation , code can read how achieve collapsable menu. can refer sample app available in google play store well.

enter image description here


Comments