现在的位置: 首页 > 综合 > 正文

ListView中嵌套GridView显示不全的解决方法

2017年11月17日 ⁄ 综合 ⁄ 共 673字 ⁄ 字号 评论关闭

项目需要,在ListView中显示多张图片,用到了GridView,不过如果使用普通的GridView,Item仅仅只是显示一部分,超出第一行以后的都无法显示了,这个很无语,所以又得继承下GridView重写onMeasure方法去测量子控件的宽高了..

这里只是贴出自定义GridView的代码,直接在xml中使用,ListView的Adapter中调用即可:

public class GridViewForListView extends GridView {
	public GridViewForListView(Context context) {
		super(context);

	}

	public GridViewForListView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
				MeasureSpec.AT_MOST);
		super.onMeasure(widthMeasureSpec, expandSpec);
	}
}

其中的MeasureSPEC.AT_MOST 等同于xml的wrap_content,让GridView能够自适应,另外,我们在使用时,也应该讲其layout_height属性设置为wrap_content

抱歉!评论已关闭.