To compute the expandable listview for a given group. - Android User Interface

Android examples for User Interface:ListView

Description

To compute the expandable listview for a given group.

Demo Code

/**/*from w w w.j  a v a2s.c  o  m*/
 * Copyright (C) 2015 Orange
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */
//package com.java2s;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;

public class Main {
    /**
     * To compute the expandable listview for a given group.
     *
     * @param listView ExpandableListView
     * @param group    group
     */
    public static void updateExpandableListViewHeight(
            ExpandableListView listView, int group) {
        ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView
                .getExpandableListAdapter();
        if (listAdapter == null) {
            // pre-condition
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getGroupCount(); i++) {
            View groupItem = listAdapter.getGroupView(i, false, null,
                    listView);
            groupItem.measure(0, 0);

            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(0, 0);
                    totalHeight += listItem.getMeasuredHeight();
                }
            }
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalHeight
                + (listView.getDividerHeight() * (listAdapter
                        .getGroupCount() - 1));
        listView.setLayoutParams(params);
        listView.requestLayout();
    }
}

Related Tutorials