Android 监听 ScrollView 滑动到最底部
转载自: https://www.cnblogs.com/carmanloneliness/p/4355158.html
做产品时,有一个需求,需要监听ScrollView滑动到最底部。在网上找了些方法,都有这样或那样的问题,要不就是监听不精确,
要不就是重复监听,那些代码没有产品化,很不可靠。
经过自己试验,终于找到了个能够精确监听的实现方法。代码如下:
public class ScrollBottomScrollView extends ScrollView {
private OnScrollBottomListener _listener;
private int _calCount;
public interface OnScrollBottomListener {
void srollToBottom();
}
public void registerOnScrollViewScrollToBottom(OnScrollBottomListener l) {
_listener = l;
}
public void unRegisterOnScrollViewScrollToBottom() {
_listener = null;
}
public ScrollBottomScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
View view = this.getChildAt(0);
if (this.getHeight() + this.getScrollY() == view.getHeight()) {
_calCount++;
if (_calCount == 1) {
if (_listener != null) {
_listener.srollToBottom();
}
}
} else {
_calCount = 0;
}
}
}
用到这段代码的产品也测试上线了,没有Bug。
Android SDK 中类似于这样的不完美数不胜数,比如onTouch事件的传递,onMeasure方法的调用,都是很不精确。