Android 监听 ScrollView 滑动到最底部。

做产品时,有一个需求,需要监听ScrollView滑动到最底部。在网上找了些方法,都有这样或那样的问题,要不就是监听不精确,

要不就是重复监听,那些代码没有产品化,很不可靠。

经过自己试验,终于找到了个能够精确监听的实现方法。代码如下:

 1 public class ScrollBottomScrollView extends ScrollView {
 2 
 3     private OnScrollBottomListener _listener;
 4     private int _calCount;
 5 
 6     public interface OnScrollBottomListener {
 7         void srollToBottom();
 8     }
 9 
10     public void registerOnScrollViewScrollToBottom(OnScrollBottomListener l) {
11         _listener = l;
12     }
13 
14     public void unRegisterOnScrollViewScrollToBottom() {
15         _listener = null;
16     }
17 
18     public ScrollBottomScrollView(Context context, AttributeSet attrs) {
19         super(context, attrs);
20     }
21 
22     @Override
23     protected void onScrollChanged(int l, int t, int oldl, int oldt) {
24         View view = this.getChildAt(0);
25         if (this.getHeight() + this.getScrollY() == view.getHeight()) {
26             _calCount++;
27             if (_calCount == 1) {
28                 if (_listener != null) {
29                     _listener.srollToBottom();
30                 }
31             }
32         } else {
33             _calCount = 0;
34         }
35     }
36 }

用到这段代码的产品也测试上线了,没有Bug。

Android SDK 中类似于这样的不完美数不胜数,比如onTouch事件的传递,onMeasure方法的调用,都是很不精确,

需要自己再优化的,没办法,无力吐槽。

原文地址:https://www.cnblogs.com/carmanloneliness/p/4355158.html