Megaetron
Megaetron

Reputation: 1192

Android | How to use ExpandableListView in a ScrollView?

I have two ExpandableListViews in my xml-file and some TextViews. When I expand the first (at the top) ExpandableListView it squeezes all content below together and the second (at the bottom) ExpandableListView has only very less space left. The user can only see the content of the second ExpandableListView by scrolling in a very little range.

I already thought of wrapping it in a ScrollView but then my ExpandableListViews only show the first group and aren't expandable. Also if I set the height to a static number and all groups have been expanded the content can't be seen anymore.

I want that both ExpandableListViews can expand and that I can scroll the layout up and down so that everything has enough space. Any advice?

My XML.File

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="?attr/colorPrimary"
            android:paddingLeft="16dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:text="Group1"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Attention1"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp" />

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/elv_theorie"
            android:layout_gravity="center_horizontal"
            android:groupIndicator="@android:color/transparent"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="?attr/colorPrimary"
            android:paddingLeft="16dp"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"
            android:paddingRight="16dp"
            android:paddingTop="16dp"
            android:text="Group2"
            android:textStyle="bold" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Attention2"
            android:textStyle="bold"
            android:textColor="@android:color/black"
            android:paddingLeft="16dp"
            android:paddingRight="16dp"
            android:paddingTop="5dp"
            android:paddingBottom="5dp"/>

        <ExpandableListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/elv_pruefung"
            android:layout_gravity="center_horizontal"
            android:groupIndicator="@android:color/transparent" />

</LinearLayout>

My problem:

My Problem

With a ScrollView: Only first group is shown and it can't be expanded

With ScrollView

Upvotes: 4

Views: 2158

Answers (2)

MurugananthamS
MurugananthamS

Reputation: 2405

Add this line inside the scrollview:
 android:fillViewport="true"

Upvotes: 2

Vimal Gajera
Vimal Gajera

Reputation: 497

You should set dynamic Height of expandable listview using this function

private void setListViewHeight(ExpandableListView listView,
                               int group) {
    ExpandableListAdapter listAdapter = (ExpandableListAdapter) listView.getExpandableListAdapter();
    int totalHeight = 0;
    int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(),
            View.MeasureSpec.EXACTLY);
    for (int i = 0; i < listAdapter.getGroupCount(); i++) {
        View groupItem = listAdapter.getGroupView(i, false, null, listView);
        groupItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
        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);
                totalHeight += listItem.getMeasuredHeight();
            }
        }
    }
    ViewGroup.LayoutParams params = listView.getLayoutParams();
    int height = totalHeight
            + (listView.getDividerHeight() * (listAdapter.getGroupCount() - 1));
    if (height < 10)
        height = 200;
    params.height = height;
    listView.setLayoutParams(params);
    listView.requestLayout();

}

Upvotes: 0

Related Questions