一. 继承图

ScrollView称为滚动视图,当在一个屏幕的像素显示不下绘制的UI控件时,可以采用滑动的方式,使控件显示。

从继承图可以看出,ScrollView原来是一个FrameLayout的容器,不过在他的基础上添加了滚动,允许显示的比实际多的内容。

二. ScrollView

1.首先,我们在一个垂直排列的 LinearLayout 的 放置五个按钮,我们会发现由于超出屏幕的显示范围,我们看不到下面部分的按钮。

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
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical">

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="内容一"
android:textColor="#03A9F4"
android:textSize="24sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容二"
android:textColor="#03A9F4"
android:textSize="24sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容三"
android:textColor="#03A9F4"
android:textSize="24sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容四"
android:textColor="#03A9F4"
android:textSize="24sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容五"
android:textColor="#03A9F4"
android:textSize="24sp"/>

</LinearLayout>

2.我们可以使用ScrollView让界面可以滚动,这样就可以滚动而看到下面的内容。

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
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="20dp"
android:gravity="center"
android:text="内容一"
android:textColor="#03A9F4"
android:textSize="24sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容二"
android:textColor="#03A9F4"
android:textSize="24sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容三"
android:textColor="#03A9F4"
android:textSize="24sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容四"
android:textColor="#03A9F4"
android:textSize="24sp"/>

<Button
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容五"
android:textColor="#03A9F4"
android:textSize="24sp"/>

</LinearLayout>

</ScrollView>

3.此时,我们需要注意,ScrollView的子元素只能有一个,可以是一个View(如ImageView、TextView等) 也可以是一个ViewGroup(如LinearLayout、RelativeLayout等),其子元素内部则不再限制,否则会报以下异常。

1
Caused by: java.lang.IllegalStateException: ScrollView can host only one direct child

三. HorizontalScrollView

在实际使用时,我们也会遇到水平方向,控件超出屏幕的情况。这时就需要使用水平方向的滚动视图HorizontalScrollView。

和 ScrollView 的用法一致,我们在上面的例子上简单的修改一下成为一个新的例子。

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
<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">

<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容一"
android:textColor="#03A9F4"
android:textSize="18sp"/>

<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容二"
android:textColor="#03A9F4"
android:textSize="18sp"/>

<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容三"
android:textColor="#03A9F4"
android:textSize="18sp"/>

<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容四"
android:textColor="#03A9F4"
android:textSize="18sp"/>

<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="80dp"
android:gravity="center"
android:text="内容五"
android:textColor="#03A9F4"
android:textSize="18sp"/>

</LinearLayout>

</HorizontalScrollView>

四. Xml中常用的属性

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
//设置拉滚动条时,边框渐变的方向。
//none(边框颜色不变)
//horizontal(水平方向颜色变淡)
//vertical(垂直方向颜色变淡)。
android:fadingEdge=""

//设置拉到尽头(底部,顶部),然后继续拉是否出现阴影效果。
//never
//always
//ifContentScrolls
android:overScrollMode="never"

//设置是否显示滚动条
//none
//horizontal:水平
//vertical:垂直
android:scrollbars=""

//设置滚动条的大小
android:scrollbarSize=""

//设置滚动条的样式
//insideInset
//insideOverlay
//outsideInset
//outsideOverlay
android:scrollbarStyle=""

//当一个View获取到焦点,定义ViewGroup和其他子控件两者之间的关系
//beforeDescendants ViewGroup会优先其子类控件而获得焦点
//afterDescendants ViewGroup只有当其子类控件不需要获取焦点时才获得焦点
//blocksDescendants ViewGroup会直接覆盖其子类控件而获得焦点
android:descendantFocusability=""

//设置ScrollView是否需要拉伸自身内容来填充ViewPort

//true false
android:fillViewport=""
//比如ScrollView嵌套的子控件高度达不到屏幕高度时,虽然ScrollView高度设置了match_parent,也无法充满整个屏幕,需设置android:fillViewport=“true"使ScrollView填充整个页面,给ScrollView设置背景颜色就能体现。

五. 常用的方法

1. 滑动开关控制

1
2
3
4
5
6
7
scrollView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
// true禁止滑动 false可滑动
return true;
}
});

2.滑动位置控制

1
2
3
4
5
6
7
8
9
10
scrollView.post(new Runnable() {
@Override
public void run() {
//滑动到顶部
scrollView.fullScroll(ScrollView.FOCUS_UP);

//滑动到底部
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});

3. 滑动到某个位置

1
2
3
4
5
6
7
8
scrollView.post(new Runnable() {
@Override
public void run() {
//偏移值
int offset = 100;
scrollView.smoothScrollTo(0, offset);
}
});

4. 滚动监听

基本的 setOnScrollListener() 的方法系统并没有帮我们实现,我们可以使用继承的方式自定义控件。

参考:Android 中实现 ScrollView 的滚动事件监听

参考文章

让你的布局滚动起来—ScrollView

[Android的ScrollView简单使用实例(附Demo)](https://blog.csdn.net/qq_36243942/article/details/82185051# 1.垂直滚动:Scroll)