【问题标题】:ExpandableListView item background color weird behaviorExpandableListView 项目背景颜色奇怪的行为
【发布时间】:2017-06-07 08:38:27
【问题描述】:

当用户触摸可扩展列表视图中的孩子时,我正在尝试设置背景颜色。我设法通过使用此代码来做到这一点:

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 组视图,如果我展开第二组,选择第二个孩子,然后展开第一组,第一组的第一个孩子的背景被着色,第二组的孩子的背景得到透明的。

当我扩展第一组而不选择任何子时:

如果我折叠第一组,第二组的孩子的背景会再次着色。

这是我的 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;
    }
}

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

【问题讨论】:

  • 所以只有一件物品你想要改变颜色?
  • 每组一个项目
  • 问题是,当您明确处理任何项目颜色可见性等时,如果您由于视图回收而未在适配器中处理它,则会出错,因此在您的情况下,您必须维护颜色的标志该项目需要更改,您可以在数据集中设置该标志并使用条件语句在适配器中处理相同
  • 这将帮助你从中获得灵感stackoverflow.com/questions/16601760/…
  • 我将 onclick 监听器移到了适配器中,并将 addChildView 和 removeChildView 方法保留在片段中,但我仍然面临同样的问题。

标签: android expandablelistview expandablelistadapter


【解决方案1】:

我将 ConciergeOptionsPairs 视为您的子数据,因此在 ConciergeOptionsPairs 类中添加一个布尔变量 isSelect,然后在您的 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;
        }

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

【讨论】:

  • 欢迎,由于列表的回收行为,我们需要在getview中处理视图状态
猜你喜欢
  • 1970-01-01
  • 2012-11-30
  • 2013-01-06
  • 2021-12-27
  • 2016-03-28
  • 1970-01-01
  • 2011-08-06
  • 2012-09-28
  • 1970-01-01
相关资源
最近更新 更多