android - [Solved]Navigation drawer selected item textColor by Custom Adapter -
may know how change selected item's text color in navigation drawer.
the android:textcolor listview not working after install appcompat_v7 22.1.1 think cannot change using selector drawable.
i manage change default color of drawer items using custom adapter.
but manage set first item green when initialized.
may know how change color of selected items(onclick) color want when item selected.
adapter:
public class navigationdraweradapter extends arrayadapter<string> { private int mselecteditem; private final context context; private string[] sections; public navigationdraweradapter(context context, int resource, string[] objects) { super(context, resource, objects); this.context = context; sections = objects; } public int getselecteditem() { return mselecteditem; } public void setselecteditem(int selecteditem) { mselecteditem = selecteditem; } private string getmenuitemtitle(int position) { if (position >= 0 && position <= sections.length) return sections[position]; return "invalid section requested"; } @override public view getview(int position, view convertview, viewgroup parent) { //get item textview textview view = (textview) super.getview(position, convertview, parent); view.settext(getmenuitemtitle(position)); if (position == mselecteditem) { view.settextcolor(getcontext().getresources().getcolor(r.color.green)); } else { view.settextcolor(getcontext().getresources().getcolor(r.color.white)); } return view; } }
navigation drawer:
mdrawerlistview.setadapter(new navigationdraweradapter( getactionbar().getthemedcontext(), android.r.layout.simple_list_item_activated_1, new string[]{ getstring(r.string.title_home), getstring(r.string.title_stamp), getstring(r.string.title_scanner), getstring(r.string.title_setting),}) { }); mdrawerlistview.setitemchecked(mcurrentselectedposition, true); return mdrawerlistview; .. ... private void selectitem(int position) { mcurrentselectedposition = position; if (mdrawerlistview != null) { mdrawerlistview.setitemchecked(position, true); } if (mdrawerlayout != null) { mdrawerlayout.closedrawer(mfragmentcontainerview); } if (mcallbacks != null) { mcallbacks.onnavigationdraweritemselected(position); } }
i had found solution.
since have extend adapter. i'm need call , change selected item position when item clicked make changes.
listdrawer onitemclicklistener:
mdrawerlistview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { selectitem(position); myadapter.setselecteditem(position); }
and custom adapter:
public void setselecteditem(int selecteditem) { mselecteditem = selecteditem; } private string getmenuitemtitle(int position) { if (position >= 0 && position <= sections.length) return sections[position]; return "invalid section requested"; } @override public view getview(int position, view convertview, viewgroup parent) { //get item textview textview view = (textview) super.getview(position, convertview, parent); view.settext(getmenuitemtitle(position)); if (position == mselecteditem) { view.settextcolor(getcontext().getresources().getcolor(r.color.green)); } else { view.settextcolor(getcontext().getresources().getcolor(r.color.normal)); } return view; }
*adapter class on question.
source : samsaodev.com
Comments
Post a Comment