【问题标题】:Modify Child Element (color) in ExpandableListView from outside of Adapter从适配器外部修改 ExpandableListView 中的子元素(颜色)
【发布时间】:2014-12-18 22:34:09
【问题描述】:

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

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

【问题讨论】:

    标签: android android-fragments expandablelistview


    【解决方案1】:

    在子元素的类中创建一个成员变量来跟踪它的颜色。然后你需要你的 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();
    

    【讨论】:

    • 我唯一的问题是如何将上一个项目的颜色改回。我认为'notifyDataSetChanged();'可以解决问题,但不幸的是我现在修改了两个项目。
    • 您可以做到这一点的一种方法是简单地循环遍历所有元素并重置它们的所有颜色。然后设置所需元素的颜色。另一种方法是记住您为其设置颜色的最后一个元素,并在每次为新元素设置颜色时重置最后一个元素。
    【解决方案2】:

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

        if (child.getId() == currentlyActiveChild) {
            txtListChild.setTextColor(Color.RED);
        } else {
            txtListChild.setTextColor(defaultChildColor);
        }
    

    显然在适配器内部我有

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

    【讨论】:

      猜你喜欢
      • 2012-02-04
      • 1970-01-01
      • 2018-01-25
      • 1970-01-01
      • 2013-11-16
      • 2013-06-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多