Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I calculate the required height of a GridView that is inside of a ScrollView on Android?

I have a GridView inside of a LinearLayout inside of a ScrollView that pages in data from the server. Beneath the GridView is a button to load more data. My GridView will have an ultimate height that is larger than the screen. If I set the height of my GridView to either wrap_content or parent_fill, it sizes itself to the exact available on-screen height and does not scroll at all, cropping out the extra rows. If I explicitly set the layout_height to something large, like 1000dip, scrolling behaves properly, however I cannot predict the final height of my scroll view apriori.

How do I programmatically determine the necessary height of a GridView to get the desired behaviour?

Here is my layout below. As you can see I set the height to 1000dip, but that is bogus, I need that value to get set automatically/programmatically:

        <ScrollView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"        
    android:layout_weight="1"       
    >   
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"    
            >

            <GridView xmlns:android="http://schemas.android.com/apk/res/android" 
                android:id="@+id/grid"
                android:layout_width="fill_parent" 
                android:layout_height="1000dip"
                android:columnWidth="70dp"
                android:numColumns="auto_fit"
                android:verticalSpacing="0dp"
                android:horizontalSpacing="0dp"
                android:stretchMode="columnWidth"
                android:gravity="center"
                android:background="#000000"
                android:layout_weight="1"
            />
            <Button
            android:id="@+id/load_more"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:text="Load More Foo" 
            />
        </LinearLayout>
    </ScrollView>
like image 951
esilver Avatar asked Dec 03 '22 11:12

esilver


1 Answers

Here is one way to do this, if someone needs it. A bit of a hack but does the trick. You have to set GridView initially big enough for all the views (e.g. 10000dip)

final GridView imageContainer = // your GridView
imageContainer.getViewTreeObserver().addOnGlobalLayoutListener( new OnGlobalLayoutListener() 
            {
            @Override
            public void onGlobalLayout() 
                {
                imageContainer.getViewTreeObserver().removeGlobalOnLayoutListener( this );
                View lastChild = imageContainer.getChildAt( imageContainer.getChildCount() - 1 );
                imageContainer.setLayoutParams( new LinearLayout.LayoutParams( LayoutParams.FILL_PARENT, lastChild.getBottom() ) );
                }
            });
like image 148
Karitsa Avatar answered Dec 11 '22 09:12

Karitsa