21

I'm trying I put the GridView inside ScrollView in android. When I put the GridView not work.

Here is layout.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/scroll_home"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout 
            android:id="@+id/layout_home" 
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">       

              <GridView xmlns:android="http://schemas.android.com/apk/res/android"
                android:id="@+id/programacao_grid"
                android:numColumns="auto_fit"
                android:gravity="center"
                android:columnWidth="50dp"
                android:stretchMode="columnWidth"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_below="@+id/calendario_programacao"
             >       
            </GridView> 

    </RelativeLayout>

</ScrollView>

After many search a found the answer and it is below

2
  • move gridview out of scrollview Jan 21, 2014 at 17:22
  • but I need that the GridView stay inside ScrollView Jan 21, 2014 at 17:26

3 Answers 3

83

After search i found this project link:-

ExpandableHeightGridView class

package xx.xxx.xx.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;

public class ExpandableHeightGridView extends GridView {

boolean expanded = false;

public ExpandableHeightGridView(Context context)
{
    super(context);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs)
{
    super(context, attrs);
}

public ExpandableHeightGridView(Context context, AttributeSet attrs,
        int defStyle)
{
    super(context, attrs, defStyle);
}

public boolean isExpanded()
{
    return expanded;
}

@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
{
    // HACK! TAKE THAT ANDROID!
    if (isExpanded())
    {
        // Calculate entire height by providing a very large height hint.
        // View.MEASURED_SIZE_MASK represents the largest height possible.
        int expandSpec = MeasureSpec.makeMeasureSpec(MEASURED_SIZE_MASK,
                MeasureSpec.AT_MOST);
        super.onMeasure(widthMeasureSpec, expandSpec);

        ViewGroup.LayoutParams params = getLayoutParams();
        params.height = getMeasuredHeight();
    }
    else
    {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }
}

public void setExpanded(boolean expanded)
{
    this.expanded = expanded;
} }

layout.xml file:

<ScrollView
    android:id="@+id/sc_spots"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true" >


        <xx.xxx.xx.view.ExpandableHeightGridView
            android:id="@+id/spotsView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="15dp"
            android:layout_marginBottom="15dp"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:horizontalSpacing="10dp"
            android:isScrollContainer="false"
            android:numColumns="5"
            android:stretchMode="columnWidth"
            android:verticalSpacing="10dp" />
 </ScrollView>

Using GridView class

mGridView = (ExpandableHeightGridView)
getView().findViewById(R.id.spotsView);
mGridView.setExpanded(true);
SpotsAdapter adapter = new SpotsAdapter(getActivity(),R.layout.spot_item,params);
mGridView.setAdapter(adapter);
adapter.notifyDataSetChanged();
6
  • 1
    I was having the same use case and this solution worked great. Sep 9, 2014 at 14:19
  • @ Igor Ronner from the above answer what SpotsAdapter,R.layout.spot_item,params stands for.?
    – Jamal
    Dec 1, 2014 at 13:40
  • great answer dude :)
    – aligur
    Apr 12, 2016 at 13:07
  • is it scrolling now or taking the entire height?
    – Suchith
    Apr 22, 2016 at 11:57
  • Thanks! Works like a charm
    – andre719mv
    Oct 1, 2016 at 23:01
9

This works for me

   // Setting on Touch Listener for handling the touch inside ScrollView

       gridView.setOnTouchListener(new View.OnTouchListener() {
                    @Override
                    public boolean onTouch(View v, MotionEvent event) {
                        v.getParent().requestDisallowInterceptTouchEvent(true);
                        return false;
                    }

                });
0

Just to mention that, the solution provided by @Igor Ronner is great, but still did a half part in my app for unknown reason -- within a scroll view, the grid view is sitting flat like it does not exist at all.

It took me almost half a day with numerous' attempts to fill the other half -- use a relative layout to wrap to grid view to let it auto grow, like this (the actual code that finally works for me!!!):

<RelativeLayout
    android:id="@+id/grid_view_box"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/text_catalog"
    >

    <com.takken.app.android.component.framework.ExpandableGridView
        android:id="@+id/grid_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:numColumns="2"
        android:horizontalSpacing="12dp"
        android:verticalSpacing="12dp"
        />

</RelativeLayout>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.