簡體   English   中英

ListView僅顯示一個高度為“ wrap_content”的元素

[英]ListView only one element shown with height = “wrap_content”

在我的片段布局上,我有一個嵌套滾動視圖,頂部和底部有兩個相對布局,在底部,我有一個約20個元素的listview,但是如果我用wrap_content設置listview的高度,則只顯示一行,而如果我將高度設置為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>

您可以使用下面的Utility方法根據子計數來設置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.刪​​除NestedScrollView
2.像這樣將其添加到android:transcriptMode =“ alwaysScroll”

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

您可以簽出此一張“ 可擴展高度列表視圖”

我個人使用了這個: Exapandable Height GridView,並且與Expandable Height List View相同

伙計,這是RelativeLayout Blitzkrieg的一種。 但是,如果這是適合您的功能所需的,也許您應該嘗試設置NestedScrollView來填充其視口。

機器人:fillViewport = “真”

    <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”版本對此進行了嘗試,並且可以正常工作,也許您也可以使用4次。

更新:您是對的,較高的方法將給您留下不可滾動的片段。

我還在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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM