滚动监听
-
滚动到底部
// 滚动到底
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
Log.i(TAG, "BOTTOM SCROLL");
} -
滚动到顶
if (scrollY == 0) {// 滚动到顶
Log.i(TAG, "TOP SCROLL");
}
完整代码如下:
svscrollouter.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
//Log.e(TAG, "onScrollChange: " + scrollX +"---" + scrollY + "----" +oldScrollX + "---" + oldScrollY );
//监听滚动状态
if (scrollY > oldScrollY) {//向下滚动
Log.i(TAG, "Scroll DOWN");
}
if (scrollY < oldScrollY) {//向上滚动
Log.i(TAG, "Scroll UP");
}
if (scrollY == 0) {// 滚动到顶
Log.i(TAG, "TOP SCROLL");
}
// 滚动到底
if (scrollY == (v.getChildAt(0).getMeasuredHeight() - v.getMeasuredHeight())) {
Log.i(TAG, "BOTTOM SCROLL");
}
//判断某个控件是否可见
Rect scrollBounds = new Rect();
svscrollouter.getHitRect(scrollBounds);
if (tvscrollthree.getLocalVisibleRect(scrollBounds)) {//可见
Log.e(TAG, "onScrollChange: 第3个可见" );
}else {//完全不可见
Log.e(TAG, "onScrollChange: 第3个不可见" );
}
// Log.e(TAG, "onScrollChange: ------------" + scrollY +"------"+ tvscrollthree.getTop() );
// //判断某个控件是否滚到顶部
// if (scrollY == tvscrollthree.getTop()){
// Log.i(TAG, "onScrollChange: ------top-------" );
// }
//
// Log.e(TAG, "onScrollChange: bottmo" + scrollY +"-----"+ (tvscrollthree.getTop() + tvscrollthree.getHeight()) );
}
});
滚动控制
- 滚动到底部
svscrollouter.fullScroll(NestedScrollView.FOCUS_DOWN);
- 滚动到顶部
svscrollouter.fullScroll(NestedScrollView.FOCUS_UP);
- 滚动到摸个控件的顶部或底部
//顶部
svscrollouter.scrollTo(0,tvscrollfour.getTop());
//底部
svscrollouter.scrollTo(0,tvscrollthree.getBottom());
判断某个子控件是否显示
//判断某个控件是否可见
Rect scrollBounds = new Rect();
svscrollouter.getHitRect(scrollBounds);
if (tvscrollthree.getLocalVisibleRect(scrollBounds)) {//可见
Log.e(TAG, "onScrollChange: 第3个可见" );
}else {//完全不可见
Log.e(TAG, "onScrollChange: 第3个不可见" );
}