How to Put Gridview Inside Scrollview

How to put GridView inside ScrollView

There are definitely benefits to a GridView beside the inherent scrolling. For example, a consistent, dynamic layout of cells that will expand and contract based on the data you pass into it. So, when people say it's not good to desire such a functionality, I think that's wrong because you could want the dynamic grid of images (views) inside of a scrolling view, but want that entire scrolling view to contain other things than just the grid cells.

Now, here is how you can do this. Check the answer here. It is an expandable height GridView, which you will want to import / create in your project. What that basically means is that as more items are added to the GridView, it will just expand its height, as opposed to keeping its height set and using scrolling. This is exactly what you want.

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

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

Then, in your activity where you set the GridView's adapter, you want to make sure you set it to expand. So:

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

The reason you want this expandable GridView is because, the fact that a standard GridView doesn't expand is what causes it to scroll. It sticks to a certain height, and then as more items fill it past its view bounds, it becomes scrollable. Now, with this, your GridView will always expand its height to fit the content within it, thus never allowing it to enter its scrolling mode. This enables you to use it inside of the ScrollView and use other view elements above or below it within the ScrollView, and have them all scroll.

This should give you the result you're looking for. Let me know if you have any questions.

Problems with GridView inside ScrollView in android

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();

How to add gridView in a scrollView in android

I thought i might add this for anyone else who comes looking for a complete answer .After implementing @innershows answer,i created the class InnerGridView and made a reference to my onCreate() in my MainActivity like:InnerGridView gridView = (InnerGridView) findViewById(R.id.grid);
adapter = new CustomListAdapter(this, movieList);
gridView.setAdapter(adapter);

Then on my activty_main:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
tools:context="com.snappy.stevekamau.cosmeticsapp.MainActivity">

<include
android:id="@+id/app_bar"
layout="@layout/app_bar"></include>

<ImageView
android:id="@+id/imageTop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_below="@+id/app_bar"

android:layout_marginLeft="3dp"
android:layout_marginRight="3dp"

android:src="@drawable/fairnessteaser" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/imageTop"
android:layout_below="@+id/app_bar"
android:background="@drawable/red_button"
style="@style/button_text"
android:layout_alignBaseline="@+id/imageTop"
android:text="Explore" />

<TextView
android:id="@+id/whatsNew"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageTop"
android:text="What's new" />

<com.snappy.stevekamau.cosmeticsapp.InnerGridView
android:id="@+id/grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="@+id/whatsNew"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="3dp"
android:listSelector="@drawable/list_row_selector"
android:numColumns="3"
android:padding="3dp"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" />

</RelativeLayout>
</ScrollView>
<fragment
android:id="@+id/fragment_navigation_drawer"
android:name="com.snappy.stevekamau.cosmeticsapp.NavigationDrawerFragment"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="@layout/fragment_navigation_drawer"
tools:layout="@layout/fragment_navigation_drawer"></fragment>

Flutter gridview inside listview

You can try to use Horizontal scrolling lists inside a vertical scrolling list to achieve this type of layout easily.

In your horizontal list and vertical list put

shrinkWrap : true

Or

Wrap the lists inside an Expanded() widget

EDIT

This is how I used a List view inside another list view.

                     Container ( child : 
ListView.builder(
itemBuilder: (context, subMenuIndex) {
return Container(
padding: EdgeInsets.only(left: 20.0),
child: sideMenuStuff.sideMenuData.elementAt(mainIndex).menu.subMenu.elementAt(subMenuIndex).subSubMenu != null
? ExpansionTile(
title: Text(sideMenuStuff.sideMenuData.elementAt(mainIndex).menu.subMenu.elementAt(subMenuIndex).zero.info.title,
),
children: <Widget>[
ListView.builder(
shrinkWrap: true,
itemCount: sideMenuStuff.sideMenuData.elementAt(mainIndex).menu.subMenu.elementAt(subMenuIndex).subSubMenu.length,
itemBuilder:
(context, subSubMenuIndex) {
return Container(
padding: EdgeInsets.only(
left: 40.0,
top: 10.0,
bottom: 10.0),
child:
GestureDetector(
onTap:
() {
Navigator
.pop(context);
Navigator
.of(context)
.push(MaterialPageRoute(
builder: (context) =>
Products(
sideMenuStuff
.sideMenuData
.elementAt(
mainIndex)
.menu
....
....
);


Related Topics



Leave a reply



Submit