不需要监听滑动位置,只需要重写ScrollView的onOverScrolled和stopNestedScroll方法就可以了
public class ReadScrollView extends ScrollView{ private int mScrollY = 0; private boolean mClampedY = false; public ReadScrollView(Context context) { super(context); } public ReadScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public ReadScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } /** * 监听变化---一般用不到 * @param scrollX * @param scrollY * @param clampedX * @param clampedY */ // @Override // protected void onScrollChanged(int l, int t, int oldl, int oldt) { // super.onScrollChanged(l, t, oldl, oldt); // if (scrollChangedListener != null){ // scrollChangedListener.onScrollChanged(l,t,oldl,oldt); // } //// DebugUtil.d("l==" + l +" t==" + t +" oldl==" + oldl + " oldt=="+oldt); // } @Override protected void onOverScrolled(int scrollX, int scrollY, boolean clampedX, boolean clampedY) { super.onOverScrolled(scrollX, scrollY, clampedX, clampedY); //scrollY=位置0和最底y坐标;clampedY=是否到顶部或者底部 mScrollY = scrollY; mClampedY = clampedY; // DebugUtil.d("======l==onOverScrolled"+" scrollY="+scrollY+" clampedY="+clampedY ); } /** * 刷新到顶部和底部 */ @Override public void stopNestedScroll() { super.stopNestedScroll(); if (mClampedY){ if (mScrollY == 0){ scrollChangedListener.onScrollChangedTop(); }else { scrollChangedListener.onScrollChangedBottom(); } } DebugUtil.d("======l==stopNestedScroll" ); } OnScrollChangedListener scrollChangedListener; public void setScrollChangedListener(OnScrollChangedListener scrollChangedListener) { this.scrollChangedListener = scrollChangedListener; } /** * */ public interface OnScrollChangedListener{ //到达顶部 void onScrollChangedTop(); //到达底部 void onScrollChangedBottom(); //监听变化 // void onScrollChanged(int l,int t,int oldl,int oldt); } }
使用
scrollview.setScrollChangedListener(new ReadScrollView.OnScrollChangedListener() { @Override public void onScrollChangedTop() { ShowUtils.toast("顶部"); } @Override public void onScrollChangedBottom() { ShowUtils.toast("底部"); } });
原文地址:https://www.cnblogs.com/Jieth/p/9192611.html
时间: 2024-11-08 18:29:16