简体   繁体   中英

How to change the height of ExpandableListView when group is expanded

How to change the height of ExpandableListView when group is expanded/collapsed?

I want to change the height of ExpandableListView to fit the height of its content, so that the view will not scroll inside itself.

this is the layout for main activity.

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="jp.foo.android.activity.FooActivity"
    tools:ignore="MergeRootFrame" />

and this is a layout for the fragment of the main activity.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="jp.foo.android.fragment.FooFragment"
    android:background="@color/category_bg">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/frameLayout">

        <TextView
            android:text="@string/subtitle_question_category"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/subtitle_category"
            android:gravity="center_vertical|center_horizontal"
            android:layout_gravity="center_horizontal|top"
            android:textSize="20sp" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/frameLayout"
        android:background="@drawable/category_list_bg_shape"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:minHeight="300dp">

    <LinearLayout
        android:id="@+id/category_list_progressbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical"
        android:visibility="gone">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

    <ExpandableListView
        android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/category_list"
            android:transcriptMode="normal">

        </ExpandableListView>
        </FrameLayout>

</RelativeLayout>

Below is the actual layout.
ExpandableListView will scroll inside when it has too many contents.
I want to scroll whole screen. Not only scroll view, but with header. 布局样本

I solved a similar issue putting everything into a Header. I was close to solving it the traditional ways (ie. nesting layouts, weights, ...) but I felt the solution was pretty dirty, requiring overriding methods such as onGroupClick etc... to disable the ExpandableListView's scroll, while the header thing was clean and worked flawlessly for me.

So if you want to try this approach, just put an ExpandableListView alone inside your fragment, and prepare the rest to be inserted as Header.

So in your fragment a full ExpandableListView :

 <ExpandableListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/category_list"
    android:transcriptMode="normal">
 </ExpandableListView>

Then create the layout expandable_header.xml :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="jp.foo.android.fragment.FooFragment"
    android:background="@color/category_bg">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:layout_centerHorizontal="true"
        android:id="@+id/frameLayout">

        <TextView
            android:text="@string/subtitle_question_category"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/subtitle_category"
            android:gravity="center_vertical|center_horizontal"
            android:layout_gravity="center_horizontal|top"
            android:textSize="20sp" />
    </FrameLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/frameLayout"
        android:background="@drawable/category_list_bg_shape"
        android:layout_marginTop="0dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:layout_marginBottom="10dp"
        android:minHeight="300dp">

    <LinearLayout
        android:id="@+id/category_list_progressbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center"
        android:gravity="center_horizontal|center_vertical"
        android:orientation="vertical"
        android:visibility="gone">
        <ProgressBar
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</RelativeLayout>

And then inflate the header into the ExpandableListView:

.
.
.
View view = View.inflate(context, R.layout.expandable_header, null);
expandableList.addHeaderView(view, null, false);
.
.
.

This way the header will scroll together with the ExpandableListView , as it's inside it.

BTW, your layout has redundant elements, for example the FrameLayout that contains only a TextView . You can use only the TextView . Also the LinearLayout with the ProgressBar , if you are not using an opaque background, you could put the ProgressBar alone. The less ViewGroups you use the better the layout will perform!

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