Steps to Highlight the selected item in the ExpandableListView
To use setItemChecked we need to Set the checked state
of the specified position. The is only valid if the choice mode has been set to
CHOICE_MODE_SINGLE
or CHOICE_MODE_MULTIPLE
.
listSelector is used to indicate the currently selected
item in the list.
4.
setItemChecked with the list position we get in step 2.
5.
Create row_highlighter.xml file.
xml version="1.0"
encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/holo_orange_dark"
android:state_activated="true"/>
<item android:drawable="@android:color/transparent"/>
</selector>
6.
Add android:background="@drawable/row_highlighter" attribute to child.xml of expandableListView.
7.
Source Code:
expandableListView.setOnChildClickListener(new
OnChildClickListener() {
public boolean onChildClick(ExpandableListView parent, View v,
int
groupPosition, int childPosition, long id) {
int index =
parent.getFlatListPosition(ExpandableListView
.getPackedPositionForChild(groupPosition,
childPosition));
parent.setItemChecked(index, true);
return false;
}
});
output:
Improvements
If you want to see an example of customizing navigation drawer and highlighting of expanadablelistview then, see Custom Navigation Drawer post. This example supports from API levels.
If you have any other quick hints that you think people will find useful, feel free to leave a comment.