I’m using an ExpandableListView in a left nav for a tablet screen.
When a user presses a child of a group in my expandable list, I’d like to keep the child in the pressed state so that the user knows for which child the right hand content is being shown for.
For a ListView, I was able to accomplish this effect with this line in the code:
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
and then applying this selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@drawable/menu_item_pressed" />
<item android:state_pressed="false" android:state_focused="false" android:drawable="@drawable/menu_item_normal" />
<item android:state_pressed="true" android:drawable="@drawable/menu_item_pressed" />
<item android:state_focused="true" android:state_pressed="false" android:drawable="@drawable/menu_item_pressed" />
The “state_activiated” is true on the listView item after the listView item is pressed.
I was hoping this same technique would work for my expandableListView but it hasn’t. I used:
getExpandableListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
and then used the same selector above, but it doesn’t work. I’ve also tried other states such as state_selected and state_checked and those don’t work either.
The selector above correctly applies the pressed and not pressed state. It looks like however with an ExpandableListView, the state is not “activated” after pressing a child.
Any help is appreciated. Thanks!
I ended up solving this problem without using a selector on the child view. The adapter for the expandableListView takes data for each group and child. I added a “selected” boolean for the child data. The fragment takes care of correctly setting each child’s selected boolean to either true or false. Then, in the adapter’s “getChildView” method, if the child’s “selected” boolean is true, I set the view’s background resource to my “pressed” background. Otherwise I set it to the “not pressed” background. Doing this in addition to using a selector for the textView on the child view to change the text color when pressed achieves the desired effect.
Below are the things my Fragment does to maintain the child’s “selected” boolean:
In addition to the above I maintain a currentGroupPosition and currentChildPosition as class variables. Using setRetainInstance = true allows this to work properly on things like screen rotates.