S
S
Sergey Semenko2015-10-19 15:34:05
Java
Sergey Semenko, 2015-10-19 15:34:05

Android - How to calculate the height of a GridView?

Let's say we have a populated GridView with auto-fit columns (auto_fit) that is in a ScrollView. I set the layout_height="wrap_content" parameter, but the height of the list turns out to be equal to the height of one element, and I need it to shrink to a size that will fit the entire list.
Markup code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="ru.redo.zona.MainTabFragment">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/scrollView"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <RelativeLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/main_container">

            <LinearLayout
                android:orientation="vertical"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/main_popularmovies"
                android:layout_alignParentTop="true"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true">

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:gravity="center_horizontal"
                    android:id="@+id/main_popularmovies_title">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:textAppearance="?android:attr/textAppearanceMedium"
                        android:text="@string/main_topfilms"
                        android:id="@+id/main_popularmovies_titleText"/>

                </LinearLayout>

                <GridView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/main_popularmovies_list"
                    android:stretchMode="columnWidth"
                    android:verticalSpacing="10dp"
                    android:numColumns="auto_fit"
                    android:horizontalSpacing="10dp"
                    android:columnWidth="@dimen/cover_width"
                    android:gravity="center"/>

            </LinearLayout>

        </RelativeLayout>
    </ScrollView>

</RelativeLayout>

An example of what I want to do:
ee3394c349be47238cd896a75fdbf12b.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
belozerow, 2015-10-19
@abler98

You most likely have the wrong markup, so you are trying to go the wrong way.
What you are trying to do is killing the whole idea of ​​gridview.
Look for ready-made solutions for adding headers to gridview

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question