Android: ScrollView 中的 Listview 具有动态高度

Android: Listview in ScrollView with dynamic height

目前我正在开发滚动视图中的设置菜单。基本结构如下所示:

<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>

对于列表视图我使用了一个自定义实现,给它一个固定的高度并使其能够在滚动视图中滚动(注意:我必须在滚动视图中使用列表视图,因为我有很多元素,所以使用线性布局不会很好)。

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();
}

}

listview的每一项都包含一个label和一个gridview,都显示正确。但是现在我想实现这个功能,用户可以点击标签来显示和隐藏 gridview 的可见性。

但是如果我点击标签显示gridview,listview的高度不适应新打开的元素。它始终保持在同一高度,如果我打开一个元素,下面的元素将不再可见。如果我调用列表视图的 requestLayout() 方法再次触发它的 onMeasure,这甚至也会发生。

有人知道我做错了什么吗?

编辑:更新了我的 xml 文件 :)

最后我只能通过将 Listview 替换为 LinearLayout 并将我的项目添加到布局中来解决这个问题。