Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
AndroidのScrollViewの中のGridViewのlayout_heightが伸び縮みしないのを独自Viewで克服
package xx.xxx.xx.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.GridView;
/**
* ScrollViewの中のGridViewでも高さを可変にする<br>
* http://stackoverflow.com/questions/8481844/gridview-height-gets-cut
*/
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;
}
}
<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>
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();
@ulohani90
Copy link

ulohani90 commented Feb 27, 2015

HI I used this Expandableheightgridview. It works fine on Android L but crashes on 4.4 and below.
Error comes at line 48: super.onMeasure(widthMeasureSpec, expandSpec);

Error-: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams

Cant figure out why is this happening. Please help me!!!!

@SilverFoxA
Copy link

SilverFoxA commented Jun 6, 2015

I have got the same issue too

@dharmakshetri
Copy link

dharmakshetri commented Jul 17, 2015

I could not detected onscroll change listener.

@engr-erum
Copy link

engr-erum commented Aug 29, 2015

how to use two gridviews inside scrollview but gridview should contain header and footer

@mimoccc
Copy link

mimoccc commented Jun 6, 2016

The problem is that You must set allways the LayoutParams as a parent of children is.
Example:

CardView inside LinearLayout should call setLayoutParams(new LinearLayout.LayoutParams(...))
Thats logic, right?
try to override function with instanceof, example :
if(getLayoutParams() != null) && (getLayoutParams() instanceof LinearLayoutParams) { ... }
I know this is most ugliest code, say thanx to private & final properties & methods of google team

@inveni0
Copy link

inveni0 commented Feb 3, 2017

I'm having trouble with this view on Kindle Fire. On my regular Android device, I can see the whole grid view, but on Kindle Fire, the bottom 5-10% gets clipped off, and I can't scroll further to see it all. I imagine this is a difference in screen measuring, but I don't know enough about it to figure out how to fix it. Any ideas?

@vasush
Copy link

vasush commented Nov 15, 2017

I am getting null pointer exception :Attempt to invoke virtual method 'void ExpandableHeightGridView.setExpanded(boolean)' on a null object reference.
Please help.

@ajmalpk23
Copy link

ajmalpk23 commented Nov 2, 2021

any solution for onscroll change listener.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment