如何在父选定时更改expandablelistview中的父背景颜色?

时间:2015-07-29 05:00:40

标签: android

我希望在expandablelistview中展开时更改父级所选位置的背景颜色,当我再次按下父级进行折叠时,它会再次更改所选位置的背景颜色。

我尝试了下面的代码,但是当我展开一个父级然后展开第二个父级然后应用程序崩溃并显示空指针exeaption时,它只适用于一个父代。

  exp_family.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
        @Override
        public void onGroupExpand(int groupPosition) {


            Log.e("expanded Position", "" + groupPosition);


                exp_family.getChildAt(groupPosition).findViewById(R.id.rl_detail_member).setBackgroundResource(R.drawable.gradient_bg);
                exp_family.getChildAt(groupPosition).findViewById(R.id.ic_exp_col).setBackgroundResource(R.drawable.ic_collaps_detail);

        }
    });

    exp_family.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
        @Override
        public void onGroupCollapse(int groupPosition) {
            Log.e("collapse Position", "" + groupPosition);

                exp_family.getChildAt(groupPosition).findViewById(R.id.rl_detail_member).setBackgroundColor(Color.WHITE);
                exp_family.getChildAt(groupPosition).findViewById(R.id.ic_exp_col).setBackgroundResource(R.drawable.ic_expand);
        }
    }); 

4 个答案:

答案 0 :(得分:3)

将此代码应用于ExpandableListAdapter文件

getGroupView()方法,其中包含可扩展列表的父级布局

getGroupView方法

中写下以下代码

此处我在群组展开或折叠时更改了图像

if(isExpanded)
{
   //You can Apply your code here like below code for change Background when group expanded
   img.setImageResource(R.drawable.less_sign);
}
else
{
   //You can Apply your code here like below code for change Background when group not expanded(collapsed)
   img.setImageResource(R.drawable.more_sign);
}

答案 1 :(得分:2)

在覆盖方法内部getGroupView()写入

@Override
public View getGroupView(final int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (isExpanded) {
                convertView.setBackgroundResource(USE YOUR BACKGROUND ON SELECTED);
            } else {
                convertView.setBackgroundResource(USE YOUR BACKGROUND ON UNSELECTED);
            }
}

答案 2 :(得分:1)

ExpandableListview适配器中传递exp_family的参考资料。然后在适配器检查。

if(exp_family.isGroupExpanded(position)){
    //Change color
} else {
   //Change back to previous color
}

如果尝试设置展开的背景,则设置默认背景。

答案 3 :(得分:0)

你可以轻松地做到。在drawable文件夹中创建一个选择器,例如

<item android:drawable="@drawable/log_gradient_bg" android:state_pressed="false" android:state_selected="false"/>
<item android:drawable="@drawable/log_gradient_bg_hover" android:state_pressed="true"/>
<item android:drawable="@drawable/log_gradient_bg_hover" android:state_pressed="false" android:state_selected="true"/>

然后添加它expandablelistview父级背景 机器人:背景= “@绘制/ log_spendable_list_selector”

你可以看到代码 https://github.com/RimonGazi/Android/blob/master/IPAEPS/res/drawable/log_spendable_list_selector.xmlhttps://github.com/RimonGazi/Android/blob/master/IPAEPS/res/layout/log_spendable_parent.xml