码迷,mamicode.com
首页 > 移动开发 > 详细

Android: ScrollView监听滑动到顶端和底端

时间:2014-07-16 21:17:38      阅读:168      评论:0      收藏:0      [点我收藏+]

标签:android   style   blog   color   代码   re   

在项目中需要监听ScrollView滑动到顶端和底端的时候以实现自己的ScrollView,那么怎样去监听呢?今天查看了一下ScrollView的源码,找到了一种方法。先看一下源码中的overScrollBy()方法:

 1 protected boolean overScrollBy(int deltaX, int deltaY,
 2             int scrollX, int scrollY,
 3             int scrollRangeX, int scrollRangeY,
 4             int maxOverScrollX, int maxOverScrollY,
 5             boolean isTouchEvent) {
 6         final int overScrollMode = mOverScrollMode;
 7         final boolean canScrollHorizontal =
 8                 computeHorizontalScrollRange() > computeHorizontalScrollExtent();
 9         final boolean canScrollVertical =
10                 computeVerticalScrollRange() > computeVerticalScrollExtent();
11         final boolean overScrollHorizontal = overScrollMode == OVER_SCROLL_ALWAYS ||
12                 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollHorizontal);
13         final boolean overScrollVertical = overScrollMode == OVER_SCROLL_ALWAYS ||
14                 (overScrollMode == OVER_SCROLL_IF_CONTENT_SCROLLS && canScrollVertical);
15         int newScrollX = scrollX + deltaX;
16         if (!overScrollHorizontal) {
17             maxOverScrollX = 0;
18         }
19         int newScrollY = scrollY + deltaY;
20         if (!overScrollVertical) {
21             maxOverScrollY = 0;
22         }
23         // Clamp values if at the limits and record
24         final int left = -maxOverScrollX;
25         final int right = maxOverScrollX + scrollRangeX;
26         final int top = -maxOverScrollY;
27         final int bottom = maxOverScrollY + scrollRangeY;
28         boolean clampedX = false;
29         if (newScrollX > right) {
30             newScrollX = right;
31             clampedX = true;
32         } else if (newScrollX < left) {
33             newScrollX = left;
34             clampedX = true;
35         }
36         boolean clampedY = false;
37         if (newScrollY > bottom) {
38             newScrollY = bottom;
39             clampedY = true;
40         } else if (newScrollY < top) {
41             newScrollY = top;
42             clampedY = true;
43         }
44         onOverScrolled(newScrollX, newScrollY, clampedX, clampedY);
45         return clampedX || clampedY;
46     }

此方法有9个参数,参数太多有点让人模糊。这里我们不管他们是什么意思,直接看36-43行代码,很显然这是一个垂直方向的边界检查,而这也正是我们想要的。因此我们可以重写此方法,在此方法中实现监听,代码如下:

 1  @Override
 2     protected boolean overScrollBy(int deltaX, int deltaY, int scrollX, int scrollY, int scrollRangeX, int scrollRangeY, int maxOverScrollX, int maxOverScrollY, boolean isTouchEvent) {
 3 
 4         int newScrollY = scrollY + deltaY;
 5         final int bottom = maxOverScrollY + scrollRangeY;
 6         final int top = -maxOverScrollY;
 7         if (newScrollY > bottom) {
 8             System.out.println("滑动到底端");
 9         }
10         else if (newScrollY < top) {
11             System.out.println("滑动到顶端");
12         }
13         return super.overScrollBy(deltaX, deltaY, scrollX, scrollY, scrollRangeX, scrollRangeY, maxOverScrollX, maxOverScrollY, isTouchEvent);
14     }

是不是很简单呢?

Android: ScrollView监听滑动到顶端和底端,布布扣,bubuko.com

Android: ScrollView监听滑动到顶端和底端

标签:android   style   blog   color   代码   re   

原文地址:http://www.cnblogs.com/hgfrzh/p/3836248.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!