简体   繁体   中英

Android Change Background of Parent when Button in Child is clicked in Expandable ListView

I have an ExpandableListView which shows messages, in the childview you can see the text and a button to accept the message that you have read it. When clicking the button I want to change the backgroundcolor of the parentview.

I used a adapter but i dont know how to change the view of the parent in the onclick method of the accept button. Here is my adapter so far:

     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;
    }

    }  

edit: I found some way to get it, but its not how I want it. The background only changes when I click a second time on the parent to colapse it. Another try was to call getGroupView inside of the onClick methode but I couldnt get it to work.

您可以尝试:

parent.setBackgroundColor(color);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM