ScrollView滚动视图

ScrollView滚动视图:

用于给普通组件添加一个垂直滚动条,最多包含一个组件,ScrollView与Swing中的JScrollPanel类似

甚至不能称为容器,只是为其他容器添加滚动条

水平方向的滚动条要使用:HorizontalScrollView

实现ScrollView垂直滚动和HorizontalScrollView水平滚动的视图

只需要在main.xml文件中创建

代码:

main.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
android:layout_width="match_parent"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="@+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="@string/hello_world" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:text="@string/hello_world" />

</LinearLayout>
</HorizontalScrollView>
</ScrollView>

代码分析:

就是用ScrollView和HorizontalScrollView包裹住我们的容器LinearLayout,然后就可以了.

运行截图:

二.当然我们也可以在MainActivity中设置一些属性

实例:在java文件中设置scrollView的初始位置

代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
private ScrollView scroll;  
private HorizontalScrollView hscroll;
private LinearLayout line;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

scroll = (ScrollView) findViewById(R.id.scroll);
hscroll = (HorizontalScrollView) findViewById(R.id.hscroll);
line = (LinearLayout) findViewById(R.id.LinearLayout1);

scroll.scrollTo(0,line.getBottom());
hscroll.scrollTo(line.getWidth(), 0);
}

这里的话我们利用.scrollTo方法去设置滚动条的起始位置,参数分别为x,y轴坐标

运行结果如上图,这里略过…


在此之前,经历过ScrollView嵌套ScrollView滑动冲突,ScrollView与ListView嵌套滑动冲突等等,网上解决方法比较多暂不赘述了,RecyclerView出来已经差不多2年了,ListView可以说已经成为了过去式了,现在开发完全使用RecyclerView就行了。

我亲自试验过,RecyclerView嵌套RecyclerView是不存在滑动冲突的,ScrollView嵌套RecyclerView也会存在显示不全的问题,滑动也有一点点粘连的感觉不是太流畅,NestedScrollView嵌套RecyclerView不会存在显示不全的问题,代码如下:

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
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/app_name"/>
</LinearLayout>

</android.support.v4.widget.NestedScrollView>

上面的代码只是简单的嵌套而已,但是还有一个小问题,触摸到RecyclerView的时候滑动还有些粘连的感觉,只需要在代码中设置:

1
2
mRecyclerView.setNestedScrollingEnabled(false);
mRecyclerView.setHasFixedSize(false);

就可以完美的解决这个问题。

转自:https://www.jianshu.com/p/791c0a4acc1c