经过(太多)研究,我偶然发现了尼尔·特拉夫特(Neil Traft)的出色回答。
适应他的工作GridView已经很容易了。
ExpandableHeightGridView.java:
package com.example;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; }}
将其包含在您的布局中,如下所示:
<com.example.ExpandableHeightGridView android:id="@+id/myId" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" android:horizontalSpacing="2dp" android:isScrollContainer="false" android:numColumns="4" android:stretchMode="columnWidth" android:verticalSpacing="20dp" />
最后,您只需要让它扩展即可:
mAppsGrid = (ExpandableHeightGridView) findViewById(R.id.myId);mAppsGrid.setExpanded(true);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)