Selector Expandable List View - Api 8+
I'm trying to add a selector to my expandable listview I have a background, text color and and image that I need to change state when the item is selected here's my layout for the
Solution 1:
Setting up the adapter
ArrayList<MenuRow> menu = new ArrayList<MenuRow>();
menu.add(new MenuChild(MenuPosition.DASHBOARD, getString(R.string.dashboard)));
menu.add(new MenuChild(MenuPosition.HISTORY, getString(R.string.history)));
MenuGroup group= new MenuGroup(MenuPosition.MY_GROUP,
getString(R.string.my_group));
group.items.add(new MenuChild(MenuPosition.CHILD_ITEM,
getString(R.string.child_item)));
group.items.add(new MenuChild(MenuPosition.CHILD_ITEM_2,
getString(R.string.child_item2)));
menu.add(group);
menu.add(new MenuChild(MenuPosition.SETTINGS, getString(R.string.settings)));
adapter = new MenuAdapter(context, menu);
myExpandableListView.setAdapter(adapter);
The adapter
publicclassMenuAdapterextendsBaseExpandableListAdapter {
private Context context;
private ArrayList<MenuRow> menu;
privateintselectedGroupIndex=0;
privateintselectedChildIndex=0;
publicMenuAdapter(Context context, ArrayList<MenuRow> menu) {
this.context = context;
this.menu = menu;
}
publicvoidsetSelectedGroupIndex(int selectedGroupIndex) {
this.selectedGroupIndex = selectedGroupIndex;
notifyDataSetChanged();
}
publicbooleanisGroupSelected(int position) {
returnposition== selectedGroupIndex;
}
publicintgetSelectedGroupIndex() {
return selectedGroupIndex;
}
publicvoidsetSelectedChildIndex(int selectedChildIndex) {
this.selectedChildIndex = selectedChildIndex;
notifyDataSetChanged();
}
publicbooleanisChildSelected(int position) {
returnposition== selectedChildIndex;
}
publicintgetSelectedItemIndex() {
return selectedChildIndex;
}
@Overridepublic Object getChild(int groupPosition, int childPosition) {
MenuRowrow= menu.get(groupPosition);
if (row.isShownAsGroup()) {
ArrayList<MenuChild> childs = ((MenuGroup) row).getItems();
return childs.get(childPosition);
} else {
returnnull;
}
}
publiclonggetChildId(int groupPosition, int childPosition) {
return childPosition;
}
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View view, ViewGroup parent) {
MenuChildchild= (MenuChild) getChild(groupPosition, childPosition);
if (view == null) {
LayoutInflaterinfalInflater= (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = infalInflater.inflate(R.layout.menu_child_item, null);
}
TextViewtxtMenu= (TextView) view.findViewById(R.id.txt_menu);
txtMenu.setText(child.getTitle());
if (groupPosition == selectedGroupIndex
&& childPosition == selectedChildIndex) {
txtMenu.setTextColor(context.getResources().getColor(
R.color.turquoise));
} else {
txtMenu.setTextColor(context.getResources().getColor(
R.color.blue_grey));
}
return view;
}
publicintgetChildrenCount(int groupPosition) {
MenuRowrow= menu.get(groupPosition);
if (row.isShownAsGroup()) {
return ((MenuGroup) row).items.size();
} else {
return0;
}
}
public Object getGroup(int groupPosition) {
return menu.get(groupPosition);
}
publicintgetGroupCount() {
return menu.size();
}
publiclonggetGroupId(int groupPosition) {
return groupPosition;
}
@SuppressWarnings("deprecation")@SuppressLint("NewApi")public View getGroupView(int groupPosition, boolean isLastChild, View view,
ViewGroup parent) {
MenuRowrow= menu.get(groupPosition);
MenuPositionmenuPosition= row.getMenuPosition();
if (view == null) {
LayoutInflaterinf= (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inf.inflate(R.layout.menu_group_item, null);
}
RelativeLayoutrootLayout= (RelativeLayout) view
.findViewById(R.id.root_layout);
TextViewtxtMenuItem= (TextView) view.findViewById(R.id.txt_menu);
ImageViewicon= (ImageView) view.findViewById(R.id.ic_menu);
ImageViewdropDownMenuIndicator= (ImageView) view
.findViewById(R.id.ic_drop_down_arrow);
txtMenuItem.setText(row.getTitle());
if (row.isShownAsGroup()) {
dropDownMenuIndicator.setVisibility(View.VISIBLE);
} else {
dropDownMenuIndicator.setVisibility(View.GONE);
}
StateListDrawable iconStates;
if (groupPosition == selectedGroupIndex) {
booleanisActive=true;
// Needed for correct images onPressedStates
txtMenuItem.setTextColor(getTextStates(isActive));
iconStates = getIconStates(menuPosition, isActive);
rootLayout.setBackgroundColor(context.getResources().getColor(
R.color.light_grey));
} else {
booleanisActive=false;
// Needed for correct images onPressedStates
txtMenuItem.setTextColor(getTextStates(isActive));
iconStates = getIconStates(menuPosition, isActive);
rootLayout.setBackgroundColor(context.getResources().getColor(
R.color.white));
}
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN) {
icon.setBackgroundDrawable(iconStates);
} else {
icon.setBackground(iconStates);
}
return view;
}
publicbooleanhasStableIds() {
returntrue;
}
publicbooleanisChildSelectable(int arg0, int arg1) {
returntrue;
}
private ColorStateList getTextStates(boolean isActive) {
intressourceId=0;
if (isActive) {
ressourceId = R.color.selector_menu_item_txt_selected;
} else {
ressourceId = R.color.selector_menu_item_txt;
}
return context.getResources().getColorStateList(ressourceId);
}
private StateListDrawable getIconStates(MenuPosition menuPosition,
boolean isActive) {
StateListDrawablestates=newStateListDrawable();
switch (menuPosition) {
case DASHBOARD:
states.addState(
newint[] { android.R.attr.state_pressed },
context.getResources().getDrawable(
R.drawable.ic_menu_dashboard_active));
if (isActive) {
states.addState(newint[] {}, context.getResources()
.getDrawable(R.drawable.ic_menu_dashboard_active));
} else {
states.addState(newint[] {}, context.getResources()
.getDrawable(R.drawable.ic_menu_dashboard));
}
break;
case HISTORY:
states.addState(
newint[] { android.R.attr.state_pressed },
context.getResources().getDrawable(
R.drawable.ic_menu_history_active));
if (isActive) {
states.addState(newint[] {}, context.getResources()
.getDrawable(R.drawable.ic_menu_history_active));
} else {
states.addState(newint[] {}, context.getResources()
.getDrawable(R.drawable.ic_menu_history));
}
break;
case SETTINGS:
states.addState(
newint[] { android.R.attr.state_pressed },
context.getResources().getDrawable(
R.drawable.ic_menu_settings_active));
if (isActive) {
states.addState(newint[] {}, context.getResources()
.getDrawable(R.drawable.ic_menu_settings_active));
} else {
states.addState(newint[] {}, context.getResources()
.getDrawable(R.drawable.ic_menu_settings));
}
break;
default:
break;
}
return states;
}
}
MenuPosition Enum
publicenumMenuPosition {
DASHBOARD(0, -1), HISTORY(1, -1), SETTINGS(2, -1);
privatefinalint childPosition;
privatefinalint groupPosition;
MenuPosition(int groupPosition, int childPosition) {
this.groupPosition = groupPosition;
this.childPosition = childPosition;
}
publicintgetGroupIndex(){
return groupPosition;
}
publicintgetChildIndex(){
return childPosition;
}
}
The getIconStates
and getTextStates
are for if you want to add other states than just selected/unselected, in my case I wanted to add state_pressed
I tried using a ColorStateList but with no avail so I used this workaround
selector_menu_item_txt
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:state_pressed="true"android:color="@color/turquoise"/><itemandroid:color="@color/blue_grey"/></selector>
selector_menu_item_txt_selected
<?xml version="1.0" encoding="utf-8"?><selectorxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:color="@color/turquoise"/></selector>
Post a Comment for "Selector Expandable List View - Api 8+"