简体   繁体   English

Android - ExpandableListView更改子项的背景

[英]Android - ExpandableListView change background for child items

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. 我在我的应用程序中使用ExpandableListView ,我从用户那里得到的一个抱怨是,当List项目被展开时,很难在视觉上区分子项目结束和下一个组项目开始的位置。

So I would like to change the background of the child List item to the different shade. 所以我想将子List项的背景更改为不同的阴影。

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. 到目前为止,我所做的残酷尝试都是基于直接更改子View项目中元素的背景颜色和文本,但这会导致丢失和突出显示。 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. 我尝试了stylesselectors但真正让我感到困惑的是,如果我更改了子项目的背景,那么我需要为焦点/启用等所有组合添加selectors 。当我试图覆盖一件事时。

Is there a way to inherit parent style and set background only for non-focused, enabled child item with other styles retained? 是否有一种方法可以继承父style并仅为非重点启用的子项设置background ,并保留其他styles

Well. 好。 Here's what worked for me: 这对我有用:

  1. Create list_background.xml in your project's res/drawable directory 在项目的res/drawable目录中创建list_background.xml
  2. Set background value of the top level layout of your child item to the drawable above. 将子项的顶级布局的背景值设置为上面的drawable。 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 如果你在这一点上摸不着头脑 - 我指的是在ExpandableListAdapter#getChildView创建可扩展列表的子视图时使用的xml布局文件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. 我不得不复制list_highlight_active.xmllist_highlight_inactive.xml/android-sdk-windows-1.6_r1/platforms/android-1.5/data/res/drawable到我的项目的绘制目录。 @color/item_body is just a shade of gray @color/item_body只是一片灰色

I search for similar solution. 我寻找类似的解决方案。 You can try get parent View on adapter method getChieldView. 您可以尝试在适配器方法getChieldView上获取父View。 I think this parent is a parent view of your all chields. 我认为这个父母是你所有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. Droidin有什么是正常的解决方案:为您自己的背景选择器提供您想要的确切颜色。 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. 但如果你想要的只是一点色差,那么有一种更简单的方法:alpha混合。 Set your background color with an alpha value. 使用Alpha值设置背景颜色。 For example, set background to "#BBFFFFFF". 例如,将background设置为“#BBFFFFFF”。 The first two digits indicate the alpha level. 前两位数字表示alpha级别。 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. 双赢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM