标签:
http://stackoverflow.com/questions/3738965/android-detect-when-scrollview-has-finished-scrolling-and-bouncing-back
ScrollView 用的是scroller做的滑动, 但是 scroller 被scrollView隐藏了 你无法调用 你可以直接自己写个Scrollview 代码完全copy 自带的scrollView 然后把scroller暴露出来!
可以派生的。如果(mScroller.computeScrollOffset()返回false则表示滚动结束了。
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ScrollView; public class ObservableScrollView extends ScrollView { private OnScrollChangeListener onScrollChangeListener = null; private OnScrollStopListener onScrollStopListener = null; private OnBorderListener onBorderListener = null; private boolean isBorderMonitor = false; private boolean isStart = false; public ObservableScrollView(Context context) { this(context, null); // TODO Auto-generated constructor stub } public ObservableScrollView(Context context, AttributeSet attrs) { super(context, attrs); initObservableScrollView(); // this(context, attrs, com.android.internal.R.attr.scrollViewStyle); // TODO Auto-generated constructor stub } public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initObservableScrollView(); // TODO Auto-generated constructor stub } private void initObservableScrollView() { } @Override public boolean dispatchTouchEvent(MotionEvent ev) { //requestDisallowInterceptTouchEvent(disallowIntercept); //disallowIntercept=true不让父元素拦截元素;disallowIntercept=false,没有拦截 getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); } public void setOnScrollStopListener(OnScrollStopListener onScrollStopListener) { this.onScrollStopListener = onScrollStopListener; } public void setOnScrollChangeListener( OnScrollChangeListener onScrollChangeListener) { this.onScrollChangeListener = onScrollChangeListener; } public void setOnBorderListener(OnBorderListener onBorderListener) { this.isBorderMonitor = true; this.onBorderListener = onBorderListener; } private void onScrollFinished(){ if(null != onScrollStopListener){ onScrollStopListener.onScrollStop(this); } } private void onScrollStart(int l, int t) { if (!isStart) { isStart = true; new ScrollListener(l,t).start(); } } class ScrollListener extends Thread { int oldX, oldY; public ScrollListener(int oldx, int oldy) { this.oldX = oldx; this.oldY = oldy; } public void run() { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } while (isStart) { int newX = getScrollX(); int newY = getScrollY(); if (newX == oldX && newY == oldY) { onScrollFinished(); isStart = false; } else { oldX = newX; oldY = newY; } } } } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { onScrollStart(l,t); super.onScrollChanged(l, t, oldl, oldt); if (onScrollChangeListener != null) { onScrollChangeListener.onScrollChanged(this, l, t, oldl, oldt); } if (isBorderMonitor) { if (0 == t && null != onBorderListener) { onBorderListener.onBottom(); } else if ((getChildAt(0).getMeasuredHeight() <= t + getHeight()) && null != onBorderListener) { onBorderListener.onTop(); } } } public interface OnScrollChangeListener { public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); } public interface OnScrollStopListener { public void onScrollStop(ObservableScrollView scrollView); } /** * OnBorderListener, Called when scroll to top or bottom * * @author Myth 2013-5-22 */ public static interface OnBorderListener { /** * Called when scroll to bottom */ public void onBottom(); /** * Called when scroll to top */ public void onTop(); } public void setBorderMonitor(boolean isBorderMonitor) { this.isBorderMonitor = isBorderMonitor; } }
import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.ScrollView; public class ObservableScrollView extends ScrollView { private OnScrollChangeListener onScrollChangeListener = null; private OnScrollStopListener onScrollStopListener = null; private OnBorderListener onBorderListener = null; private boolean isBorderMonitor = false; private boolean isStart = false; public ObservableScrollView(Context context) { this(context, null); // TODO Auto-generated constructor stub } public ObservableScrollView(Context context, AttributeSet attrs) { super(context, attrs); initObservableScrollView(); // this(context, attrs, com.android.internal.R.attr.scrollViewStyle); // TODO Auto-generated constructor stub } public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initObservableScrollView(); // TODO Auto-generated constructor stub } private void initObservableScrollView() { } @Override public boolean dispatchTouchEvent(MotionEvent ev) { //requestDisallowInterceptTouchEvent(disallowIntercept); //disallowIntercept=true不让父元素拦截元素;disallowIntercept=false,没有拦截 getParent().requestDisallowInterceptTouchEvent(true); return super.dispatchTouchEvent(ev); } public void setOnScrollStopListener(OnScrollStopListener onScrollStopListener) { this.onScrollStopListener = onScrollStopListener; } public void setOnScrollChangeListener( OnScrollChangeListener onScrollChangeListener) { this.onScrollChangeListener = onScrollChangeListener; } public void setOnBorderListener(OnBorderListener onBorderListener) { this.isBorderMonitor = true; this.onBorderListener = onBorderListener; } @Override public boolean onTouchEvent(MotionEvent ev) { if(ev.getAction() == MotionEvent.ACTION_UP){ // 手指松开. if (null != onScrollStopListener) { onScrollStopListener.onScrollStop(this); } } return super.onTouchEvent(ev); } @Override protected void onScrollChanged(int l, int t, int oldl, int oldt) { super.onScrollChanged(l, t, oldl, oldt); if (onScrollChangeListener != null) { onScrollChangeListener.onScrollChanged(this, l, t, oldl, oldt); } if (isBorderMonitor) { if (0 == t && null != onBorderListener) { onBorderListener.onBottom(); } else if ((getChildAt(0).getMeasuredHeight() <= t + getHeight()) && null != onBorderListener) { onBorderListener.onTop(); } } } public interface OnScrollChangeListener { public void onScrollChanged(ObservableScrollView scrollView, int x, int y, int oldx, int oldy); } public interface OnScrollStopListener { public void onScrollStop(ObservableScrollView scrollView); } /** * OnBorderListener, Called when scroll to top or bottom * * @author Myth 2013-5-22 */ public static interface OnBorderListener { /** * Called when scroll to bottom */ public void onBottom(); /** * Called when scroll to top */ public void onTop(); } public void setBorderMonitor(boolean isBorderMonitor) { this.isBorderMonitor = isBorderMonitor; } }
public class CustomScrollView extends ScrollView { private OnOverScrolledListener mOnOverScrolledListener = null; /** * @param context */ public CustomScrollView(Context context) { super(context); // TODO Auto-generated constructor stub } /** * @param context * @param attrs */ public CustomScrollView(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } /** * @param context * @param attrs * @param defStyle */ public CustomScrollView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } /* (non-Javadoc) * @see android.widget.ScrollView#arrowScroll(int) */ @Override public boolean arrowScroll(int arg0) { // TODO Auto-generated method stub return super.arrowScroll(arg0); } /* (non-Javadoc) * @see android.widget.ScrollView#computeScroll() */ @Override public void computeScroll() { // TODO Auto-generated method stub super.computeScroll(); } /** * @return the mOnOverScrolledListener */ public OnOverScrolledListener getOnOverScrolledListener() { return mOnOverScrolledListener; } /** * @param mOnOverScrolledListener the mOnOverScrolledListener to set */ public void setOnOverScrolledListener(OnOverScrolledListener mOnOverScrolledListener) { this.mOnOverScrolledListener = mOnOverScrolledListener; } public interface OnOverScrolledListener{ public abstract void onOverScrolled(View view, int scrollX, int scrollY, boolean clampedX, boolean clampedY); } /* (non-Javadoc) * @see android.widget.ScrollView#onOverScrolled(int, int, boolean, boolean) */ @Override protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { // TODO Auto-generated method stub super.onOverScrolled(scrollX, scrollY, clampedX, clampedY); if(mOnOverScrolledListener != null) { mOnOverScrolledListener.onOverScrolled(this, scrollX, scrollY, clampedX, clampedY); } Log.i("CustomScrollView", "===DBG:onOverScrolled"); } }
标签:
原文地址:http://www.cnblogs.com/exmyth/p/4523408.html