ScrollView与ListView,GridView冲突问题

在一个布局文件中 ,既有GridView还有其他元素的情况下,如果GridView数据太多,会将显示内容撑出屏幕以外,无法预览,也无法滚动.因此我们需要在布局的最外层添加一个ScrollView使其可以滚动.

 

但是这样做了之后,GridView的高度被固定了,gridview的内容比较多时,数据会显示不全.

 

解决方案:

自定义子类继承ListView和GridView,重写onMeasure方法

import android.content.Context;
import android.util.AttributeSet;
import android.widget.GridView;

public class InnerGridView extends GridView {

	public InnerGridView(Context context) {
		super(context);
	}

	public InnerGridView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

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

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

 

 

 参考:

http://www.2cto.com/kf/201210/159540.html

猜你喜欢

转载自cn-arthurs.iteye.com/blog/2157833