ExpandableListView 项目背景颜色奇怪的行为

ExpandableListView item background color weird behavior

我正在尝试在用户触摸 child 内部可扩展列表视图时设置背景颜色。我设法通过使用以下代码做到了这一点:

ArrayList<ChildView> childViewList;

ExpandableListView expandablelistview_options = (ExpandableListView) view.findViewById(R.id.expandablelistview_options);

expandablelistview_options.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{
    int index = removeChildView(groupPosition);
    ArrayList<ConciergeOptionsPairs> children=data.Options.get(groupPosition).childrens;

    if (index != childPosition)
    {
        v.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.orange));
        v.invalidate();
        addChildView(new ChildView(groupPosition, childPosition, v, children.get(childPosition)));
    }

    return false;
}
});

private int removeChildView(int groupPosition)
    {
        int index = 0;
        int childpos = 0;
        boolean exists = false;
        for (ChildView c : childViewList)
        {
            if (c.groupPosition == groupPosition)
            {
                c.view.setBackgroundColor(ContextCompat.getColor(getActivity(),R.color.transparent));
                c.view.invalidate();
                childpos = c.childPosition;
                exists = true;
                break;
            }
            index++;
        }

        if (exists)
        {
            childViewList.remove(index);
            return childpos;
        }

        return -1;
    }

private void addChildView(ChildView child)
{
    childViewList.add(child);
}

一切正常,除了这种奇怪的行为:

我有 2 组视图,如果我展开第二组,选择第二个 child,然后展开第一组,第一组的第一个 child 的背景会变色,第二组child的背景变透明

当我展开第一个组而不选择任何 child :

如果我折叠第一组,第二组 child 的背景会再次变色。

这是我的 ExpandableListView 适配器代码:

public class ConciergeOptionsExpandableListAdapter extends BaseExpandableListAdapter
{
    private Context context;
    private ArrayList<ConciergeOptions> data;
    private HotelStay hotelStay;
    private int lastExpandedGroupPosition;

    public ConciergeOptionsExpandableListAdapter(Context context, ArrayList<ConciergeOptions> data) {
        this.context = context;
        this.data = data;
        hotelStay=HotelStay.getInstance(context);
    }

    @Override
    public int getGroupCount() {
        return this.data.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.data.get(groupPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent)
    {
        ConciergeOptions options = (ConciergeOptions) getGroup(groupPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.concierge_options_item, null);
        }

        RelativeLayout container = (RelativeLayout) convertView.findViewById(R.id.container);
        TextView option_name = (TextView) convertView.findViewById(R.id.option_name);
        ImageView arrow = (ImageView) convertView.findViewById(R.id.arrow);

        option_name.setText(options.name);

        if(isExpanded){
            convertView.setBackgroundResource(R.color.bluebutton);
        }else{
            convertView.setBackgroundResource(R.color.transparent);
        }

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return (this.data.get(groupPosition).childrens.size());
    }


    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        ArrayList<ConciergeOptionsPairs> children=this.data.get(groupPosition).childrens;
        if(children!=null){
            return children.get(childPosititon);
        }
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }


    @Override
    public View getChildView(final int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.concierge_options_inner_items, null);
        }

        TextView option_type = (TextView) convertView.findViewById(R.id.option_type);
        TextView price = (TextView) convertView.findViewById(R.id.price);
        TextView decimal = (TextView) convertView.findViewById(R.id.decimal);
        TextView currency = (TextView) convertView.findViewById(R.id.currency);

        ConciergeOptionsPairs option_pair = (ConciergeOptionsPairs) getChild(groupPosition, childPosition);
        option_type.setText(option_pair.name);

        double t_price = Double.parseDouble(option_pair.value);
        double dec = t_price - (long) t_price;
        long decim = (long) dec;

        int decimal_int = (int) t_price;

        price.setText("+" + String.valueOf(decimal_int));
        decimal.setText("." + String.valueOf(decim));
        currency.setText("$");

        return convertView;
    }

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }
}

知道如何解决这个问题吗?

我将 ConciergeOptionsPairs 视为您的子数据,因此在 ConciergeOptionsPairs 中添加一个布尔变量 isSelect class 然后在你的 onChildClick

@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id)
{

    ArrayList<ConciergeOptionsPairs> children=data.Options.get(groupPosition).childrens; //i consider this is your child view array, need to be same dataset which used as child array
    int count=0;
   for(ConciergeOptionsPairs objChild:children)
   {
      if(count==childPosition)
         objChild.isSelect=true;
      else 
       objChild.isSelect=false;
    count++;

   }

   yourexpandableadpater.notifyDataSetChanged(); 


    return false;
}
});

现在在 适配器getChildView

      @Override
        public View getChildView(final int groupPosition, final int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {

            if (convertView == null) {
                LayoutInflater infalInflater = (LayoutInflater) this.context
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = infalInflater.inflate(R.layout.concierge_options_inner_items, null);
            }

            TextView option_type = (TextView) convertView.findViewById(R.id.option_type);
            TextView price = (TextView) convertView.findViewById(R.id.price);
            TextView decimal = (TextView) convertView.findViewById(R.id.decimal);
            TextView currency = (TextView) convertView.findViewById(R.id.currency);

            ConciergeOptionsPairs option_pair = (ConciergeOptionsPairs) getChild(groupPosition, childPosition);
            option_type.setText(option_pair.name);

            double t_price = Double.parseDouble(option_pair.value);
            double dec = t_price - (long) t_price;
            long decim = (long) dec;

            int decimal_int = (int) t_price;

            price.setText("+" + String.valueOf(decimal_int));
            decimal.setText("." + String.valueOf(decim));
            currency.setText("$");
            if(option_pair.isSelect)
convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.orange));
    else
convertView.setBackgroundColor(ContextCompat.getColor(context,R.color.transparent));
            return convertView;
        }

这是我对你的代码的理解,你可以用这个检查