1、自定义ScrollView
public class MyScrollView extends NestedScrollView {
private boolean noScroll = true;
//定义一个接口的对象
private ScrollViewListener scrollViewListener = null;
public MyScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setNoScroll(boolean noScroll) {
this.noScroll = noScroll;
}
@Override
public boolean onTouchEvent(MotionEvent arg0) {
if (noScroll) {
return false;
} else {
return super.onTouchEvent(arg0);
}
}
@Override
public boolean onInterceptTouchEvent(MotionEvent arg0) {
if (noScroll)
return false;
else
return super.onInterceptTouchEvent(arg0);
}
//对外提供接口回调的方法
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
//scrollview的起始点+总高度==crollView的computeVerticalScrollRange
// if(getScrollY() + getHeight() == computeVerticalScrollRange())
// {
// LogUtil.d("------滚动到最下方------");
// if(scrollViewListener!=null){
// scrollViewListener.onScrollChanged();
// }
// }
int scrollY = getScrollY();
View onlyChild = getChildAt(0);
if (onlyChild.getHeight() <= scrollY + getHeight()) { // 如果满足就是到底部了
LogUtil.d("------滚动到最下方------");
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged();
}
}
}
}
2、监听ScrollView滚动到底部
mScrollView.setNoScroll(true);
mScrollView.setScrollViewListener(new ScrollViewListener() {
@Override
public void onScrollChanged() {
//5秒后之后,发送,每隔5秒发送一次
mScrollDisposable = Observable.interval(5, 5, TimeUnit.SECONDS)
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
mScrollView.scrollTo(0, 0);
scrollPosition = 0;
mScrollDisposable.dispose();
mScrollDisposable = null;
}
});
}
});
3、一次滚动的距离
//设置定时器自动滚动
public void startAutoPost() {
if (mAutoTaskRight != null && !mAutoTaskRight.isDisposed()) {
mAutoTaskRight.dispose();
}
mAutoTaskRight = Observable.interval(1, 1, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribe(new Consumer<Long>() {
@Override
public void accept(Long aLong) throws Exception {
scrollPosition += 50;
LogUtil.d("scrollPosition:" + scrollPosition);
mScrollView.scrollTo(0, scrollPosition);
}
});