可扩展列表视图中的RadioButton

时间:2017-10-23 08:51:59

标签: android expandablelistview

我有一个ExpandableListView工作expandableListView。现在我想在组名旁边插入一个RadioButton,但是当我这样做时,扩展布局的监听器不起作用expandableListView with button

1 个答案:

答案 0 :(得分:0)

我在复杂的可扩展视图中遇到了类似的问题。您必须创建ExpandableListView.OnGroupClickListener。

public class ExpandableListViewListener implements ExpandableListView.OnGroupClickListener {

        private ExpandableListView expandableListView;
        private Context context;

    public ExpandableListViewListener(ExpandableListView expandableListView, Context context) {
        this.expandableListView = expandableListView;
        this.context = context;
    }

    /**
    * Method that set the height of the expandableView
    * @param listView to be expanded
    * @param group    index of the group tobe expanded
    */
    private void setListViewExpanded(ExpandableListView listView, int group) {

        int totalHeight = 0;

        /*
        * It checks GroupView height and, if expanded, relative children
        * It sets final result as listview height.
        * NOTE: it must be inserted into a card for a correct height calculation between different
        * API versions
        */
        ExpandableListAdapter listAdapter = listView.getExpandableListAdapter();
        int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.EXACTLY);

        /*
        * 2 nested cycles are performed:
        * one for groupItems (headers)
        * > one nested for childItems (when header is expanded)
        */
        for (int i = 0; i < listAdapter.getGroupCount(); i++) {

            View groupItem = listAdapter.getGroupView(i, false, null, listView);
        groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

            //add current groupItem height to total count
            totalHeight += groupItem.getMeasuredHeight();

            if (((listView.isGroupExpanded(i)) && (i != group))
                || ((!listView.isGroupExpanded(i)) && (i == group))) {

                for (int j = 0; j < listAdapter.getChildrenCount(i); j++) {

                    View listItem = listAdapter.getChildView(i, j, false, null, listView);
                    listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);

                    // add expanded child height
                    totalHeight += (int) context.getResources().getDimension(R.dimen.space_s) + listItem.getMeasuredHeight();

                }
            }
        }

        //setting tot height
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}