Problems with GridView inside ScrollView in android
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;
}
});
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();