最简单方法就是重写ListVIew的onMeasure方法,请看下面的代码:
public class ListViewForScrollView extends ListView {
public ListViewForScrollView(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public ListViewForScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public ListViewForScrollView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
/**
* 重写该方法,达到使ListView适应ScrollView的效果
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,
MeasureSpec.AT_MOST);
super.onMeasure(widthMeasureSpec, expandSpec);
}
}</pre><pre>
同理GridView在ScrollView中的使用也是跟ListView一样,onMeasure方法跟上面的ListVIew是一样的。
原文:http://blog.csdn.net/zhangweiwtmdbf/article/details/43056619