ScrollView中的ListView、GridView

介绍ScrollView中放ListView的文章着实不少,总结起来的话主要分两种
1、动态设置ListView的大小
2、重写ListView
本文目的是用最简单的方法实现我们想要的效果。


最简单方法就是重写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>

需要注意的是ScrollView需要调用mScrollView.smoothScrollTo(0, 0);

同理GridView在ScrollView中的使用也是跟ListView一样,onMeasure方法跟上面的ListVIew是一样的。




猜你喜欢

转载自blog.csdn.net/zhangweiwtmdbf/article/details/43056619