How to set gridview inside scrollview

I need to design a layout so that the whole layout should scroll and inside the layout I had to display the related content in the form of a Grid , so I decided to use a GridView . But the problem is that Im could not use the GridView inside the ScrollView I read the documentation (The documentation also says that we also should not use the GridView inside the ScrollView ). But I have to do this, so please give me some idea about this. thank

+65
android gridview scrollview
Oct 12 '12 at 12:37
source share
7 answers

There are certain advantages to GridView besides the built-in scrolling. For example, a sequential dynamic cell layout that will expand and contract based on the data you pass into it. Therefore, when people say that it’s not good to want such functionality, I think this is wrong, because you can want a dynamic grid of images (views) inside the scroll, but you want the whole scrollable view to contain other things, and not just the cell grid.

Now, here is how you can do it. Check the answer here . This is the extensible GridView height that you will want to import / create in your project. This basically means that as you add more elements to the GridView, it will simply increase its height, as opposed to saving its height and using scroll. This is exactly what you want.

Once you have the ExpandableHeightGridView project in your project, go to your XML layout where you want the GridView. Then you can do something like this (to rephrase):

 <ScrollView ...> <RelativeLayout ...> <com.example.ExpandableHeightGridView ... /> <other view items /> </RelativeLayout> </ScrollView> 

Then, in your activity where you install the GridView adapter, you want you to install it for the extension. So:

 ExpandableHeightGridView gridView = (ExpandableHeightGridView) findViewById(R.id.myId); gridView.setAdapter(yourAdapter); gridView.setExpanded(true); 

The reason you want this extensible GridView is because the fact that the standard GridView is not extensible causes it to hang. It sticks to a certain height, and then when the number of elements fills it beyond its visibility, it becomes scrollable. Now, with this, your GridView will always expand its height to match the content inside it, so it never allowed it to enter its scroll mode. This allows you to use it inside a ScrollView and use other view elements above or below it in a ScrollView and scroll through them all.

This will give you the result you are looking for. Let me know if you have any questions.

+108
Oct 12
source share

I know I'm late, but I have another solution that I have to use that works flawlessly. Here, the method calculates the height of the GridView depending on the number of elements it contains, and sets the height in the GridView at run time.

 public void setGridViewHeightBasedOnChildren(GridView gridView, int columns) { ListAdapter listAdapter = gridView.getAdapter(); if (listAdapter == null) { // pre-condition return; } int totalHeight = 0; int items = listAdapter.getCount(); int rows = 0; View listItem = listAdapter.getView(0, null, gridView); listItem.measure(0, 0); totalHeight = listItem.getMeasuredHeight(); float x = 1; if( items > columns ){ x = items/columns; rows = (int) (x + 1); totalHeight *= rows; } ViewGroup.LayoutParams params = gridView.getLayoutParams(); params.height = totalHeight; gridView.setLayoutParams(params); } 

After you called setAdapter in your gridview, just call

 setGridViewHeightBasedOnChildren( <yourGridView> , <no of grid view columns> ) 

and it will work.

You can define gridview in your xml as usual,

and let the code take care of that. :)

+32
Mar 21 '14 at 10:18
source share

I found a way to provide a GridView with a fixed size inside a ScrollView and enable scrolling . This allows you to see the entire ScrollView without having to scroll through all the GridView elements, and for me it makes more sense to use ExpandableHeightGridView.

To do this, you will need to implement a new class that extends the GridView and overrides onTouchEvent () to call requestDisallowInterceptTouchEvent (true). Thus, the parent view will leave the mesh interception touch events.

GridViewScrollable.java:

 package com.example; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.GridView; public class GridViewScrollable extends GridView { public GridViewAdjuntos(Context context) { super(context); } public GridViewAdjuntos(Context context, AttributeSet attrs) { super(context, attrs); } public GridViewAdjuntos(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public boolean onTouchEvent(MotionEvent ev){ // Called when a child does not want this parent and its ancestors to intercept touch events. requestDisallowInterceptTouchEvent(true); return super.onTouchEvent(ev); } } 

Add it to your layout with the characteristics and fields you want inside the ScrollView:

 <ScrollView 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" android:isScrollContainer="true" > <com.example.GridViewScrollable android:id="@+id/myGVS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:numColumns="auto_fit" android:stretchMode="columnWidth" /> </ScrollView> 

And just get it in your activity:

 GridViewScrollable myGridView = (GridViewScrollable) findViewById(R.id.myGVS); 

I hope this helps =)

+10
Nov 10 '14 at 20:33
source share

To use gridview inside scrollview, set scrollview android: fillViewport = "true"

I know that he was late to answer here, but may be useful for some new developers.

+3
Aug 01 '16 at 11:43 on
source share

If you used dennisdrew's answer, and you have content above the grid and it is hidden when you open your view, just add the last line below

 ExpandableHeightGridView gridView = (ExpandableHeightGridView) findViewById(R.id.myId); gridView.setAdapter(yourAdapter); gridView.setExpanded(true); gridView.setFocusable(false); 
+1
Aug 07 '16 at 21:08
source share

It is better to use ExpandableHeightlistView or ExpandableHeightGridView inside scrollview .

+1
Sep 19 '17 at 7:32 on
source share

I would not cause this problem. GridView does not work well in scrollView. instead, convert gridview to recyclerview and use gridLayoutManager to solve the problem.

0
09 Oct '17 at 17:56 on
source share



All Articles