Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/206.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从适配器外部修改ExpandableListView中的子元素(颜色)_Android_Android Fragments_Expandablelistview - Fatal编程技术网

Android 从适配器外部修改ExpandableListView中的子元素(颜色)

Android 从适配器外部修改ExpandableListView中的子元素(颜色),android,android-fragments,expandablelistview,Android,Android Fragments,Expandablelistview,有两个片段: -片段A-有一个按钮 -片段B-具有exapndablelistview 两者同时可见 按下按钮后,expandablelistview的一个子元素的颜色应该会改变。我将知道它将是哪个家长和孩子id。但是如何从Fragment B类而不是BaseExpandableListAdapter访问子元素。在子元素的类中创建一个成员变量来跟踪它的颜色。然后需要ListAdapter根据这个新成员变量更改子元素的颜色。现在,当按下按钮时,只需更改此颜色变量并在ListAdapter上调用no

有两个片段: -片段A-有一个按钮 -片段B-具有exapndablelistview 两者同时可见


按下按钮后,expandablelistview的一个子元素的颜色应该会改变。我将知道它将是哪个家长和孩子id。但是如何从Fragment B类而不是BaseExpandableListAdapter访问子元素。

在子元素的类中创建一个成员变量来跟踪它的颜色。然后需要ListAdapter根据这个新成员变量更改子元素的颜色。现在,当按下按钮时,只需更改此颜色变量并在ListAdapter上调用notifyDataSetChanged()

例如:

class ChildElement {
    int color;
}

@Override
public View getView(final int position, View convertView, final ViewGroup parent) {
    ChildElement e = getItem(position);
    ViewHolder holder;
    ...
    holder.view.setBackgroundColor(e.color);
    ...
}
然后改变颜色

elements.get(position).color = newColor;
adapter.notifyDataSetChanged();

我是这样做的:我有两个变量,即默认颜色和新颜色。在getChildView中,这里是我使用的一段代码

    if (child.getId() == currentlyActiveChild) {
        txtListChild.setTextColor(Color.RED);
    } else {
        txtListChild.setTextColor(defaultChildColor);
    }
很明显,我有

public void setActiveChild(long newID) {
    currentlyActiveChild = newID;
    notifyDataSetChanged();

}

我唯一的问题是如何更改前一个项目的颜色。我以为“notifyDataSetChanged();”我会做的,但不幸的是,我现在修改了两个项目。你可以做的一个方法是简单地循环所有元素并重置它们的所有颜色。然后设置所需元素的颜色。另一种方法是记住为其设置颜色的最后一个元素,并在每次为新元素设置颜色时重置最后一个元素。