Android: Listview in ScrollView with dynamic heigh

2019-06-09 21:12发布

问题:

currently I am developing a settings menu in a scrollview. The basic structure looks like the following:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

<ScrollView
    android:id="@+id/scrollview_settings"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingTop="@dimen/paddingSmall">

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

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/transparent_white_settings"
            android:paddingTop="@dimen/paddingMedium"
            android:paddingBottom="@dimen/paddingMedium">

            <ImageView
                android:id="@+id/logo"
                android:layout_width="68dp"
                android:layout_height="wrap_content"
                android:src="@drawable/newspaper_logo_color"
                android:layout_alignParentRight="true"
                android:layout_margin="@dimen/paddingBig"
                android:adjustViewBounds="true"/>

        </RelativeLayout>

        <!-- Layout News Uebersicht -->

        <include layout="@layout/separator_line_grey"/>

        <de.basecom.noznewsapp.views.FontTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="true"
            android:textColor="@color/buttonHoverBackground"
            android:textSize="@dimen/sliding_image_kicker_font_size"
            android:paddingLeft="@dimen/paddingLarge"
            android:paddingTop="@dimen/paddingMedium"
            android:paddingBottom="@dimen/paddingMedium"
            android:text="@string/news_overview"
            android:textScaleX="@dimen/letter_spacing"
            android:textStyle="bold"
            android:gravity="center_vertical"
            android:background="@color/settings_header_color"
            android:layout_gravity="center_vertical"/>

        <include layout="@layout/separator_line_grey"/>

        <!-- News-Overview Issues -->
        <LinearLayout
            android:id="@+id/issue_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/transparent_white_settings">

            <de.basecom.noznewsapp.views.NewsScrollListView
                android:id="@+id/listview_issues"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:descendantFocusability="blocksDescendants"
                android:divider="@null"
                android:dividerHeight="0dp" />

        </LinearLayout>


        <!-- Layout Einstellungen -->
        <de.basecom.noznewsapp.views.FontTextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textAllCaps="true"
            android:textColor="@color/buttonHoverBackground"
            android:textSize="@dimen/sliding_image_kicker_font_size"
            android:paddingLeft="@dimen/paddingLarge"
            android:paddingTop="@dimen/paddingMedium"
            android:paddingBottom="@dimen/paddingMedium"
            android:text="@string/settings"
            android:textStyle="bold"
            android:textScaleX="@dimen/letter_spacing"
            android:gravity="center_vertical"
            android:background="@color/settings_header_color"
            android:layout_gravity="center_vertical"/>

        <include layout="@layout/separator_line_grey"/>

        <!-- Einstellungs- Werte -->
        <LinearLayout
            android:id="@+id/settings_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:background="@color/transparent_white_settings">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/paddingLarge">

                <de.basecom.noznewsapp.views.FontTextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@string/receive_pushmessages"
                    android:textAllCaps="true"
                    android:textScaleX="@dimen/letter_spacing"
                    android:textSize="@dimen/sliding_image_kicker_font_size"
                    android:layout_centerVertical="true"
                    android:textColor="@color/buttonBackground"/>

                <Switch
                    android:id="@+id/receive_pushmessages_switch"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="@string/empty_string"/>
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>

</ScrollView>

For the listview I used a custom implementation, to give it a fix height and make it able to scroll within the scrollview (note: I have to use the listview within the scrollview, because I have lots of elements, so that using a linearlayout wouldn't be nice).

public class CustomListView extends ListView {
public CustomListView(Context context) {
    super(context);
}

public CustomListView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public CustomListView(Context context, AttributeSet attrs,int defStyle) {
    super(context, attrs, defStyle);
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // Calculate entire height by providing a very large height hint.
    // But do not use the highest 2 bits of this integer; those are
    // reserved for the MeasureSpec mode.
    int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2, MeasureSpec.AT_MOST);
    super.onMeasure(widthMeasureSpec, expandSpec);

    ViewGroup.LayoutParams params = getLayoutParams();
    params.height = getMeasuredHeight();
}

}

Each item of the listview contains a label and a gridview, which is all shown correctly. But now I want to implement the functionality, that the user can click on the label to show and hide the visibility of the gridview.

But if I click on the label and the gridview is shown, the height of the listview doesn't adapt to the new open element. It always stays at the same height and if I open an element, the elements below are no longer visible. This also even happens, if I call the requestLayout() method of the listview to trigger its onMeasure again.

Anybody got an idea what I am doing wrong?

EDIT: Updated my xml file :)

回答1:

Finally I was only able to solve this issue by replacing Listview with a LinearLayout and add my items in code to the Layout.