Question

I have a fragment with a textView giving instructions, and below a gridView. I wanted for the textView to scroll upwards with the gridView. In other words: for textView to disappear off screen when gridView is scrolling.

My idea of how to accomplish this is to give the gridView a fixed height that would make the entire layout scrollable - including the textView. However, up until I cannot achieve this. Is there a way to do this?

Layout no scroll:

Layout no scroll

When scrolled:

When scrolled:

Any help/advice would be appreciated.

My layout XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/LightGrey"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/chooseLine"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="18dp"
        android:text="@string/chooseLine"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <GridView
        android:id="@+id/gridLayout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:columnWidth="130dp"
        android:fitsSystemWindows="false"
        android:gravity="center"
        android:horizontalSpacing="5dp"
        android:numColumns="auto_fit"
        android:paddingTop="0dp"
        android:stretchMode="columnWidth"
        android:verticalSpacing="25dp" />

</LinearLayout>
Was it helpful?

Solution

Achieving this is is not going to be trivial. What you need is ListView's capability to add a header view, but unfortunately, GridView doesn't offer that functionality.

What I have done in the past to solve this problem is to convert the GridView to a ListView, and then add the header to the ListView. I created a wrapper adapter that takes the original adapter and combines a horizontal row's worth of grid cells into a single list row.

The tricky parts include: dynamically adapting the number of columns in a row based on the width of the screen, accounting for all combinations of view types within a row and remaining empty columns in the last row, and handling click interactions properly.

OTHER TIPS

Put the whole LinearLayout in a ScrollView (How to use ScrollView in Android?) and for the GridView set android:layout_height="wrap_content"

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top