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?
Well. Here's what worked for me:
res/drawable
directory 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
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);
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.