ExpendableListView的子部分中的按钮无法正常工作

时间:2018-08-02 07:41:06

标签: android android-adapter

Screenshot

篮球队是第一个具有n个子元素的组元素。当我单击“是”或“否”或“无”时,我需要更改按钮的颜色。颜色已更改。
Please find in this screenshot

但是当我单击这些按钮时,也会自动单击第二组元素(足球队)按钮。请给我一个解决方案。我已经在此处附加了CustomExpandableAdapter。

public class CustomExpandableListAdapter extends BaseExpandableListAdapter /*implements ExpandableListView.OnChildClickListener */{

   // Button btn_yes,btn_no,btn_none;
    private Context context;
    private List<String> expandableListTitle;
    private HashMap<String, List<String>> expandableListDetail;

    public CustomExpandableListAdapter(Context context, List<String> expandableListTitle,
                                       HashMap<String, List<String>> expandableListDetail) {
        this.context = context;
        this.expandableListTitle = expandableListTitle;
        this.expandableListDetail = expandableListDetail;
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

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

        final String expandedListText = (String) getChild(listPosition, expandedListPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_item, null);
        }
        TextView expandedListTextView = (TextView) convertView
                .findViewById(R.id.expandedListItem);
           final Button btn_yes = (Button)convertView.findViewById(R.id.btn_yes);
           final Button btn_no = (Button)convertView.findViewById(R.id.btn_no);
           final Button btn_none = (Button)convertView.findViewById(R.id.btn_none);

          Log.e("listPosition",">>>"+listPosition);
          Log.e("expandedListPosition",">>>"+expandedListPosition);



        btn_yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                btn_yes.setBackgroundColor(Color.parseColor("#FFD14D"));
                btn_no.setBackgroundColor(Color.TRANSPARENT);
                btn_none.setBackgroundColor(Color.TRANSPARENT);
                String flag = "1";
                Toast.makeText(context,"listPosition>>"+listPosition,Toast.LENGTH_SHORT).show();
                Toast.makeText(context,"expandedListPosition>>"+expandedListPosition,Toast.LENGTH_SHORT).show();
            }
        });
        btn_no.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_no.setBackgroundColor(Color.parseColor("#FFD14D"));
                btn_yes.setBackgroundColor(Color.TRANSPARENT);
                btn_none.setBackgroundColor(Color.TRANSPARENT);
                Toast.makeText(context,"No",Toast.LENGTH_SHORT).show();
            }
        });
        btn_none.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btn_none.setBackgroundColor(Color.parseColor("#FFD14D"));
                btn_no.setBackgroundColor(Color.TRANSPARENT);
                btn_yes.setBackgroundColor(Color.TRANSPARENT);
                Toast.makeText(context,"None",Toast.LENGTH_SHORT).show();
            }
        });

        expandedListTextView.setText(expandedListText);
        return convertView;



    }
    /*@Override
    public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
        // use the parent to set default color to all other entries



        // v is the view within the expandable list/ListView that was clicked

        Button btn_yes = (Button)v.findViewById(R.id.btn_yes);
        Button btn_no = (Button)v.findViewById(R.id.btn_no);
        Button btn_none = (Button)v.findViewById(R.id.btn_none);

        for (int i = 0; i < v.getChildCount(); i++) {
            View child = (View) v.getChildAt(i);
            // do highlighting
        }
        return false;
    }*/


    @Override
    public int getChildrenCount(int listPosition) {
        return this.expandableListDetail.get(this.expandableListTitle.get(listPosition))
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.expandableListTitle.get(listPosition);
    }

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

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

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {


        String listTitle = (String) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.list_group, null);
        }
        TextView listTitleTextView = (TextView) convertView
                .findViewById(R.id.listTitle);
        ImageView ivGroupIndicator =(ImageView)convertView.findViewById(R.id.ivGroupIndicator);
        listTitleTextView.setTypeface(null, Typeface.BOLD);
        listTitleTextView.setText(listTitle);
        ivGroupIndicator.setSelected(isExpanded);
        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }

0 个答案:

没有答案