ListView 只有一个元素显示为 height = "wrap_content"

ListView only one element shown with height = "wrap_content"

在我的片段布局上,我有一个嵌套滚动视图,内部有两个相对布局,顶部和底部,在底部我有一个包含大约二十个元素的列表视图,但是如果我将列表视图的高度设置为 wrap_content,只显示一行,而如果我将高度设置为 1000dp,例如,显示所有行,我不想以静态方式固定高度,但我宁愿使用 wrap_content, 我能做什么?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/nested_content"
    android:clipToPadding="false"
    android:scrollbars="none"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:id="@+id/top">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="200dp"
        //some button and text view
    </RelativeLayout>
  </RelativeLayout>

  <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/bottom"
        android:layout_below="@id/top">
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/scores"
        </ListView>

    </RelativeLayout>
</RelativeLayout>
</android.support.v4.widget.NestedScrollView>

您可以使用以下实用程序方法根据 child 计数设置 ListView 的高度。

public static void setListViewHeightBasedOnChildren(ListView myListView) {
        ListAdapter adapter = myListView.getAdapter(); 
        if (myListView != null) {
           int totalHeight = 0;
           for (int i = 0; i < adapter.getCount(); i++) {
              View item= adapter.getView(i, null, myListView);
              item.measure(0, 0);
              totalHeight += item.getMeasuredHeight();
           }

           ViewGroup.LayoutParams params = myListView.getLayoutParams();
           params.height = totalHeight + (myListView.getDividerHeight() * (adapter.getCount() - 1));
           myListView.setLayoutParams(params);
        }          
    }

1.Remove NestedScrollView
2. 像这样

添加到 android:transcriptMode="alwaysScroll"
    <ListView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/scores"
        android:transcriptMode="alwaysScroll"
    </ListView>

你可以看看这个Expandable Height List View

我个人使用这个:Expandable Height GridViewExpandable Height List View.

的理念相同

伙计,这是一种 RelativeLayout Blitzkrieg。但是,如果您需要它来适应您的功能,也许您应该尝试设置 NestedScrollView 以填充其视口。

android:fillViewport="true"

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/nested_content"
        android:clipToPadding="false"
        android:scrollbars="none"
        android:fillViewport="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

用你发布的 "deblitzkrieged" 版本试过了,它起作用了,也许它对你也起作用。

更新: 你是对的,上面的方法会给你留下一个不可滚动的片段。

我还在 ListView 上设置了 android:nestedScrollingEnabled="true",但立即忘记了。 如果有人仍然对代码示例感兴趣,请参阅下面的示例。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/nested_content"
        android:clipToPadding="false"
        android:fillViewport="true"
        android:scrollbars="none">

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

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:id="@+id/top">
            <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Foo Bar"/>
        </RelativeLayout>

        <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentBottom="true"
                android:id="@+id/bottom"
                android:layout_below="@id/top">
            <ListView
                    android:nestedScrollingEnabled="true"
                    android:layout_alignParentBottom="true"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:id="@+id/scores">
            </ListView>

        </RelativeLayout>
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>