Skip to content Skip to sidebar Skip to footer

ListView Only One Element Shown With Height = "wrap_content"

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 heig

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

Solution 2:

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.


Solution 3:

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>

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


Post a Comment for "ListView Only One Element Shown With Height = "wrap_content""