使用自定义 Gridview ExpandableHeightGridView在滚动视图中添加网格视图
请按照以下示例进行操作
ExpandableHeightGridView.java
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.
// But do not use the highest 2 bits of this integer; those are
// reserved for the MeasureSpec mode.
int expandSpec = MeasureSpec.makeMeasureSpec(
Integer.MAX_VALUE >> 2, 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;
}
}
xml文件
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/scroll"
android:background="@android:color/white" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white" >
<RelativeLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@android:color/white" >
<TextView
android:id="@+id/txbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="9dp"
android:text="Textview"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#2c627e"
android:textStyle="bold" />
</RelativeLayout>
<yourpackagename of ExpandableHeight.class.ExpandableHeightGridView
android:id="@+id/gridlist"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:horizontalSpacing="10dp"
android:layout_below="@+id/header"
android:numColumns="2"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:verticalScrollbarPosition="right"
android:verticalSpacing="10dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:background="#034E6D"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:id="@+id/view_rl"
android:layout_below="@+id/gridlist" >
<!--
<TextView
android:id="@+id/newsdata_grid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/newsdatas"
android:text="juijb"
android:maxLines="3"
android:layout_marginLeft="10dp"
android:textSize="20dp" />
-->
<ImageView
android:id="@+id/datas_images"
android:layout_width="60dp"
android:layout_height="50dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="5dp"
android:adjustViewBounds="true"
android:src="@drawable/bike_icon" />
<TextView
android:id="@+id/data_grids"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/datas_images"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="VIEWS"
android:textColor="@android:color/white"
android:textSize="10dp" />
</RelativeLayout>
<View android:layout_width="fill_parent"
android:layout_height="10dp"
android:background="@android:color/white"
android:layout_below="@+id/view_rl"/>
</RelativeLayout>
在 Activity 类中
ExpandableHeightGridView gridview=(ExpandableHeightGridView)findViewById(R.id.gridlist);
gridview.setExpanded(true);
这将在滚动视图中显示一个网格视图,希望这会对您有所帮助