Android - ExpandableListView change background for child items
-
19-09-2019 - |
Question
I'm using ExpandableListView
in my app and one of the complaints I get from the users is
that when the List
item is expanded it's hard to visually distinguish where the child
item ends and next group item begins.
So I would like to change the background of the child List
item to the different shade.
Brutal attempts that I've made so far were based on directly changing background color and text of the elements inside the child View
item but that leads to loss of hovers and highlights. So my question is - what is a good strategy to achieve the above?
I tried styles
and selectors
but what really bums me is if I change background for child item then I need to add selectors
for all combinations of focus/enabled etc. When all I'm trying to do it to overwrite a single thing.
Is there a way to inherit parent style
and set background
only for non-focused, enabled child item with other styles
retained?
Solution
Well. Here's what worked for me:
- Create list_background.xml in your project's
res/drawable
directory - Set background value of the top level layout of your child item to the drawable above. If you are scratching your head at this point - I'm referring to the xml layout file that get's used when you create child view of your expandable list in
ExpandableListAdapter#getChildView
Here's complete drawable file
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true"
android:drawable="@drawable/list_highlight_active" />
<item android:state_enabled="true" android:state_selected="true"
android:state_window_focused="true"
android:drawable="@drawable/list_highlight_inactive" />
<item android:state_enabled="true" android:state_window_focused="true"
android:drawable="@color/item_body" />
<item android:drawable="@color/item_body" />
</selector>
I had to copy list_highlight_active.xml
and list_highlight_inactive.xml
from /android-sdk-windows-1.6_r1/platforms/android-1.5/data/res/drawable
to the drawable directory of my project. @color/item_body
is just a shade of gray
OTHER TIPS
I just discovered a way to "somewhat" set the background color without having to jump through the selector hoops.
As you've noted, setting the background to a solid color wipes out the selector highlights because the new background color obscures it. Droidin has what is the normal solution: provide your own background selector with the exact colors you want. It's a pain to say the least.
But if all you want is a little color differentiation then there is a simpler way: alpha blend. Set your background color with an alpha value. For example, set background to "#BBFFFFFF". The first two digits indicate the alpha level. It will all blend so the background won't be a pure white and the selection highlight won't be the normal bright orange but the children will be a different color and the highlight still works. Win-win.
I search for similar solution. You can try get parent View on adapter method getChieldView. I think this parent is a parent view of your all chields. And set background from resource to this parent. I make it like this :
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.fa_report_sub_item, null);
View parentGroup=(View) convertView.getParent();
parentGroup.setBackgroundResource(R.drawable.fa_summ_report_main_item);
}