05/30
2013

滑动到底部或顶部响应的ScrollView实现

本文主要介绍实现ScrollView滑动到底部或顶部响应的两种方式

 

关于使用可见:滚动到底部或顶部响应的ScrollView使用

Demo APK 可以方便的查看效果,在各大应用商店搜索 trinea android 下载即可,如:Google Play

 

两种实现方式的主要不同点在于判断滑动位置的地方,第一种方式在onScrollChanged函数中判断, 第二种在OnTouchListener的onTouch中判断。其他如是否滚动到边缘的判断、响应事件接口OnBorderListener、对外暴露的接口都一样。

第一种方式代码可见onScrollChanged BorderScrollView
第二种方式代码可见OnTouchListener BorderScrollView

 

1、判断是否已经滚动到顶部或底部,代码如下:

private void doOnBorderListener() {
	if (contentView != null && contentView.getMeasuredHeight() <= getScrollY() + getHeight()) {
		if (onBorderListener != null) {
			onBorderListener.onBottom();
		}
	} else if (getScrollY() == 0) {
		if (onBorderListener != null) {
			onBorderListener.onTop();
		}
	}
}

滚动到顶部判断:getScrollY() == 0
滚动到底部判断

View contentView = getChildAt(0);
contentView.getMeasuredHeight() <= getScrollY() + getHeight();

其中getChildAt表示得到ScrollView的child View,因为ScrollView只允许一个child view,所以contentView.getMeasuredHeight()表示得到子View的高度, getScrollY()表示得到y轴的滚动距离,getHeight()为scrollView的高度。当getScrollY()达到最大时加上scrollView的高度就的就等于它内容的高度了啊~

 

2、定义到达顶部和底部响应事件接口OnBorderListener,包含onTop和onBottom函数

/**
 * OnBorderListener, Called when scroll to top or bottom
 * 
 * @author Trinea 2013-5-22
 */
public static interface OnBorderListener {

	/**
	 * Called when scroll to bottom
	 */
	public void onBottom();

	/**
	 * Called when scroll to top
	 */
	public void onTop();
}

这个接口允许用户自定义到达底部和顶部的响应事件

 

第一种方式: 在onScrollChanged函数中判断,主要代码如下:

@Override
protected void onScrollChanged(int x, int y, int oldx, int oldy) {
	super.onScrollChanged(x, y, oldx, oldy);
	doOnBorderListener();
}

在ScrollView的onScrollChanged函数中判断是否到达顶部和底部,并进行相应事件调用,onScrollChanged函数在ScrollView中内容开始滚动时出发被调用。

 

 

第二种方式: 在OnTouchListener的onTouch中判断,主要代码如下:

this.onBorderTouchListener = new OnTouchListener() {

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		switch (event.getAction()) {
			case MotionEvent.ACTION_UP:
				doOnBorderListener();
				break;
		}
		return false;
	}

};

表示在touch结束后,手指拿起时执行doOnBorderListener,进行判断和响应。

 

两种方式的比较:
第一种方式精确度高,但doOnBorderListener可能执行多次,因为ScrollView滑动中,onScrollChanged总是在不停被调用,需要用户自己做并发控制,也可以参考DropDownListView的写法让用户在自定义Listener执行结束后手动调用onBottomComplete函数。

 

第二种方式不会造成doOnBorderListener多次执行,但却可能一次都没有调用。比如你快速滑动,还未到达底部手指已经松开,由于惯性滚动到底部,可能并不会执行doOnBorderListener。因为onTouch的ACTION_UP事件在手指松开已经接收到这时候却还没有到达底部。

两种方式各有优劣,大家可酌情选择。我要求准确响应,所以选择了第一种,自己再做下控制确保不会多次执行。

您可以使用这些 HTML 标签和属性: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

2 thoughts on “滑动到底部或顶部响应的ScrollView实现