ListView only one element shown with height = “wrap_content”

烈酒焚心 提交于 2021-01-29 12:43:38

问题


On my fragment layout I have a Nested Scroll view with two relative layout inside, top and bottom, on the bottom I have a listview with about twenty elements, but if I set the height of the listview with wrap_content, only one line is shown, whereas if I set the height to 1000dp, for example, all the lines are shown, I wouldn't want to fix the height in a static way but I'd rather use wrap_content, what can I do?

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


回答1:


You can use below Utility method to set the height of your ListView based on child count.

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



回答2:


1.Remove the NestedScrollView
2. add this to the android:transcriptMode="alwaysScroll" like this

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



回答3:


You can check out this one Expandable Height List View.

I personally use this one: Exapandable Height GridView and is the same philosophy with the Expandable Height List View.




回答4:


Man, this is one kind of RelativeLayout Blitzkrieg. But if it is what you need to fit your feature, maybe you should try to set the NestedScrollView to fill its viewport.

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

Tried this with a "deblitzkrieged" version of what you posted and it worked, maybe it also works 4 you.

UPDATE: You are right, the upper approach would leave you with a non scrollable fragment.

I also set android:nestedScrollingEnabled="true" on the ListView and instantly forgot about it. If someone is still interested in a code sample, see the sample below.

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



来源:https://stackoverflow.com/questions/56818541/listview-only-one-element-shown-with-height-wrap-content

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!