Sunday 24 February 2019

How to Set ListView Height Depending on the Items inside ScrollView

I'm trying to add listView inside scrollView and.

the problem is I can't set the ListView height programmatically and correctly redraw the ScrollView, as the items below it still in the old position before changing the height.

XML

    <ScrollView
            android:id="@+id/movie_detail_scroll_view"
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <RelativeLayout
                android:id="@+id/movie_detail"
                style="?android:attr/textAppearanceLarge"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:clipChildren="false">
                         ...........
                 <ListView
                    android:id="@+id/trails_list_view"
                    android:layout_width="match_parent"
                    android:layout_height="200dp"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_below="@+id/movie_videos_container"
                    android:choiceMode="none"/>
                <include
                    android:id="@+id/movie_reviews_container"
                    layout="@layout/partial_movie_reviews"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_below="@+id/trails_list_view"/>
                      ............
        </RelativeLayout>
    </ScrollView>

the method that I used to resize the listView:

/**
 * Sets ListView height dynamically based on the height of the items.
 *
 * @param listView to be resized
 * @return true if the listView is successfully resized, false otherwise
 */
public static boolean setListViewHeightBasedOnItems(ListView listView, ViewGroup container) {

    ListAdapter listAdapter = listView.getAdapter();
    if (listAdapter != null) {

        int numberOfItems = listAdapter.getCount();

        // Get total height of all items.
        int totalItemsHeight = 0;
        for (int itemPos = 0; itemPos < numberOfItems; itemPos++) {
            View item = listAdapter.getView(itemPos, null, listView);
            item.measure(0, 0);
            totalItemsHeight += item.getMeasuredHeight();
        }

        // Get total height of all item dividers.
        int totalDividersHeight = listView.getDividerHeight() * (numberOfItems - 1);

        // Set list height.
        ViewGroup.LayoutParams params = listView.getLayoutParams();
        params.height = totalItemsHeight + totalDividersHeight;
        listView.setLayoutParams(params);
        listView.requestLayout();

        //redraw the container layout.
        container.requestLayout();

        return true;

    } else {
        return false;
    }

}

the result: enter image description here

I can't get rid of this free space even if when I redraw the ScrollView

what should I do?



from How to Set ListView Height Depending on the Items inside ScrollView

No comments:

Post a Comment