转载自:http://blog.csdn.net/leechee_1986/article/details/7191068
要实现一个功能:当Scrollview滑动到最底端的时候需要触发事件加载其他数据。很多人都以为ScrollView可以像ListViev那样setOnScrollListener,其实沒那么简单,因为ScrollView压根就没有该接口,在baidu上兜了一圈没有找到合适的答案,没办法只能google去了,居然一下子解决了这个问题,还是老外比较牛,呵呵,这是我访问的网址:
注意,如果数据不满一页的话,会执行onBottom方法!通常要使用懒加载的话数据都会超过一页,所以我沒仔细考虑这个问题!
我把ScrollView封装成类了,源码如下:
package com.dodowaterfall;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ScrollView;
public class LazyScrollView extends ScrollView {
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt)
{
super.onScrollChanged(l, t, oldl, oldt);
onScrollListener.onAutoScroll(l, t, oldl, oldt);
}
private static final String tag = "LazyScrollView";
private Handler handler;
private View view;
public LazyScrollView(Context context) {
super(context);
}
public LazyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public LazyScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
// 这个获得总的高度
public int computeVerticalScrollRange()
{
return super.computeHorizontalScrollRange();
}
public int computeVerticalScrollOffset()
{
return super.computeVerticalScrollOffset();
}
private void init()
{
this.setOnTouchListener(onTouchListener);
handler = new Handler() {
@Override
public void handleMessage(Message msg)
{
// process incoming messages here
super.handleMessage(msg);
switch (msg.what)
{
case 1:
if (view.getMeasuredHeight() <= getScrollY() + getHeight())
{
if (onScrollListener != null)
{
onScrollListener.onBottom();
}
} else if (getScrollY() == 0)
{
if (onScrollListener != null)
{
onScrollListener.onTop();
}
} else
{
if (onScrollListener != null)
{
onScrollListener.onScroll();
}
}
break;
default:
break;
}
}
};
}
OnTouchListener onTouchListener = new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event)
{
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_UP:
if (view != null && onScrollListener != null)
{
handler.sendMessageDelayed(handler.obtainMessage(1), 200);
}
break;
default:
break;
}
return false;
}
};
/**
* 获得参考的View,主要是为了获得它的MeasuredHeight,然后和滚动条的ScrollY+getHeight作比较。
*/
public void getView()
{
this.view = getChildAt(0);
if (view != null)
{
init();
}
}
/**
* 定义接口
*
* @author admin
*
*/
public interface OnScrollListener {
void onBottom();
void onTop();
void onScroll();
void onAutoScroll(int l, int t, int oldl, int oldt);
}
private OnScrollListener onScrollListener;
public void setOnScrollListener(OnScrollListener onScrollListener)
{
this.onScrollListener = onScrollListener;
}
}
用的时候也很简单,通常这样使用:
waterfall_scroll.getView();
waterfall_scroll.setOnScrollListener(new OnScrollListener() {
@Override
public void onTop()
{
// 滚动到最顶端
Log.d("LazyScroll", "Scroll to top");
}
@Override
public void onScroll()
{
}
@Override
public void onBottom()
{
// 滚动到最低端
AddItemToContainer(++current_page, page_count);
}