您的位置:首页 > 移动开发 > Android开发

Android中监听ScrollView滑动停止和滑动到底部

2015-05-26 21:06 197 查看
1.监听ScrollView滑动停止:

[java] view plaincopy

/********************监听ScrollView滑动停止*****************************/

scrollView.setOnTouchListener(new OnTouchListener() {

private int lastY = 0;

private int touchEventId = -9983761;

Handler handler = new Handler() {

@Override

public void handleMessage(Message msg) {

super.handleMessage(msg);

View scroller = (View) msg.obj;

if (msg.what == touchEventId) {

if (lastY == scroller.getScrollY()) {

handleStop(scroller);

} else {

handler.sendMessageDelayed(handler.obtainMessage(touchEventId, scroller), 5);

lastY = scroller.getScrollY();

}

}

}

};

public boolean onTouch(View v, MotionEvent event) {

if (event.getAction() == MotionEvent.ACTION_UP) {

handler.sendMessageDelayed(handler.obtainMessage(touchEventId, v), 5);

}

return false;

}

private void handleStop(Object view) {

ScrollView scroller = (ScrollView) view;

scrollY = scroller.getScrollY();

}

});

/***********************************************************/

2.监听ScrollView滑动到底部:

[java] view plaincopy

package com.example.webviewdemo;

import android.content.Context;

import android.util.AttributeSet;

import android.widget.ScrollView;

public class ScrollBottomScrollView extends ScrollView {

private ScrollBottomListener scrollBottomListener;

public ScrollBottomScrollView(Context context) {

super(context);

}

public ScrollBottomScrollView(Context context, AttributeSet attrs) {

super(context, attrs);

}

public ScrollBottomScrollView(Context context, AttributeSet attrs,int defStyle) {

super(context, attrs, defStyle);

}

@Override

protected void onScrollChanged(int l, int t, int oldl, int oldt){

if(t + getHeight() >= computeVerticalScrollRange()){

//ScrollView滑动到底部了

scrollBottomListener.scrollBottom();

}

}

public void setScrollBottomListener(ScrollBottomListener scrollBottomListener){

this.scrollBottomListener = scrollBottomListener;

}

public interface ScrollBottomListener{

public void scrollBottom();

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: