在Expandable ListView中单击Child中的Button时,Android更改父项背景

时间:2014-01-08 13:31:35

标签: android listview

我有一个显示消息的ExpandableListView,在子视图中你可以看到文本,还有一个按钮来接受你已经读过它的消息。 单击按钮时,我想更改父视图的背景颜色。

我使用了适配器,但我不知道如何在接受按钮的onclick方法中更改父视图。到目前为止,这是我的适配器:

     public class WarningAdapter extends BaseExpandableListAdapter {


    public ArrayList<Warning> mWarnings;
    public LayoutInflater minflater;
    public Fragment fragment;
    public Context context;


    public WarningAdapter(ArrayList<Warning> warnings){
        mWarnings = warnings;
    }

    public void setInflater(LayoutInflater mInflater, Fragment frag) {
        this.minflater = mInflater;
        fragment = frag;
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return mWarnings.get(childPosition);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        TextView text = null;
        Button accept = null;
        Button delete = null;

        // TODO Auto-generated method stub
        if (convertView == null) {
            convertView = minflater.inflate(R.layout.warning_details, null);
        }
        text = (TextView) convertView.findViewById(R.id.tv_warning_text);
        accept = (Button) convertView.findViewById(R.id.btn_warning_accept);
        delete = (Button) convertView.findViewById(R.id.btn_warning_delete);


        accept.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                mWarnings.get(groupPosition).setIsRead(true);
            }
        });

        text.setText(mWarnings.get(groupPosition).getMessage());

        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        // TODO Auto-generated method stub
        return 1;
    }

    @Override
    public Object getGroup(int groupPosition) {
        // TODO Auto-generated method stub
        return mWarnings.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        // TODO Auto-generated method stub
        return mWarnings.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        // TODO Auto-generated method stub
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        TextView subject = null;
        TextView date = null;
        ImageView category = null;
        LinearLayout background = null;


        if (convertView == null) {
            convertView = minflater.inflate(R.layout.warning_head, null);
        }
        subject = (TextView) convertView.findViewById(R.id.tv_warning_subject);
        date = (TextView) convertView.findViewById(R.id.tv_warning_date);
        category = (ImageView) convertView.findViewById(R.id.img_warning_category);
        background = (LinearLayout) convertView.findViewById(R.id.Layout_warning_head);

        subject.setText(mWarnings.get(groupPosition).getCategory()+"sub");
        date.setText(mWarnings.get(groupPosition).getEntryDate()+"date");
        category.setImageResource(R.drawable.ic_launcher);

if(mWarnings.get(groupPosition).getIsRead()){
            background.setBackgroundColor(Color.TRANSPARENT);           
        }

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        // TODO Auto-generated method stub
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        // TODO Auto-generated method stub
        return false;
    }

    }  

编辑:我找到了一些方法来获得它,但它不是我想要的方式。当我在父级上第二次单击以将其折叠时,背景仅会更改。 另一个尝试是在onClick方法中调用getGroupView,但我无法让它工作。

1 个答案:

答案 0 :(得分:0)

你可以尝试:

parent.setBackgroundColor(color);