1. 源起
很多分页加载的组件都是继承自ScrollView,分页要检查是否滚动到底部,下拉刷新需要检测是否滚动到顶部。
2. 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
<SPAN style=
"FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 18px"
><RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
tools:context=
".MainActivity"
>
<ScrollView
android:id=
"@+id/scroll_view"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:scrollbars=
"vertical"
>
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:background=
"@android:color/darker_gray"
android:orientation=
"vertical"
>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"2000dip"
android:text=
"text"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout></SPAN>
<RelativeLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
tools:context=
".MainActivity"
>
<ScrollView
android:id=
"@+id/scroll_view"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:scrollbars=
"vertical"
>
<LinearLayout
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
android:background=
"@android:color/darker_gray"
android:orientation=
"vertical"
>
<TextView
android:layout_width=
"wrap_content"
android:layout_height=
"2000dip"
android:text=
"text"
/>
</LinearLayout>
</ScrollView>
</RelativeLayout>[java] view plaincopyprint?
<SPAN style=
"FONT-FAMILY: Microsoft YaHei; FONT-SIZE: 18px"
>
public
class
MainActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final
ScrollView view = (ScrollView) findViewById(R.id.scroll_view);
view.setOnTouchListener(
new
OnTouchListener() {
@Override
public
boolean
onTouch(View v, MotionEvent event) {
switch
(event.getAction()) {
case
MotionEvent.ACTION_DOWN:
break
;
case
MotionEvent.ACTION_MOVE:
if
(v.getScrollY() <=
0
) {
Log.d(
"scroll view"
,
"top"
);
}
else
if
(view.getChildAt(
0
).getMeasuredHeight() <= v.getHeight() + v.getScrollY()) {
Log.d(
"scroll view"
,
"bottom"
);
Log.d(
"scroll view"
,
"view.getMeasuredHeight() = "
+ view.getMeasuredHeight()
+
", v.getHeight() = "
+ v.getHeight()
+
", v.getScrollY() = "
+ v.getScrollY()
+
", view.getChildAt(0).getMeasuredHeight() = "
+ view.getChildAt(
0
).getMeasuredHeight());
}
break
;
default
:
break
;
}
return
false
;
} });
}
}</SPAN>
public
class
MainActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final
ScrollView view = (ScrollView) findViewById(R.id.scroll_view);
view.setOnTouchListener(
new
OnTouchListener() {
@Override
public
boolean
onTouch(View v, MotionEvent event) {
switch
(event.getAction()) {
case
MotionEvent.ACTION_DOWN:
break
;
case
MotionEvent.ACTION_MOVE:
if
(v.getScrollY() <=
0
) {
Log.d(
"scroll view"
,
"top"
);
}
else
if
(view.getChildAt(
0
).getMeasuredHeight() <= v.getHeight() + v.getScrollY()) {
Log.d(
"scroll view"
,
"bottom"
);
Log.d(
"scroll view"
,
"view.getMeasuredHeight() = "
+ view.getMeasuredHeight()
+
", v.getHeight() = "
+ v.getHeight()
+
", v.getScrollY() = "
+ v.getScrollY()
+
", view.getChildAt(0).getMeasuredHeight() = "
+ view.getChildAt(
0
).getMeasuredHeight());
}
break
;
default
:
break
;
}
return
false
;
} });
}
}
|
3. 说明
检测滚动的时机有很多,这里是在ScrollView外面检测,使用onTouch的MotionEvent.ACTION_MOVE。如果是继承ScrollView,可以在其他回调的时候检测。
v.getScrollY()表示scrollview的上面滚出去的长度。v.getHeight()表示scrollview本身看上去的高度。view.getChildAt(0).getMeasuredHeight()是内容的高度。这里不能用view.getMeasureHeight()。
view.getMeasuredHeight() = 1038
v.getHeight() = 1038
v.getScrollY() = 2962
view.getChildAt(0).getMeasuredHeight() = 4000