Android:ScrollView 与 NestedScrollView
Android: ScrollView vs NestedScrollView
ScrollView
和 NestedScrollView
有什么区别?他们两个,扩展FrameLayout
。我想深入了解两者的优缺点。
NestedScrollView
顾名思义,当需要在另一个滚动视图中使用滚动视图时使用。通常这很难完成,因为系统无法决定滚动哪个视图。
这就是 NestedScrollView
的用武之地。
NestedScrollView
NestedScrollView is just like ScrollView, but it supports acting as
both a nested scrolling parent and child on both new and old versions
of Android. Nested scrolling is enabled by default.
https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html
ScrollView
Layout container for a view hierarchy that can be scrolled by the
user, allowing it to be larger than the physical display. A
ScrollView is a FrameLayout, meaning you should place one child in it
containing the entire contents to scroll; this child may itself be a
layout manager with a complex hierarchy of objects
https://developer.android.com/reference/android/widget/ScrollView.html
除了nested scrolling NestedScrollView
added one major functionality, which could even make it interesting outside of nested contexts: It has build in support for OnScrollChangeListener
. Adding a OnScrollChangeListener
to the original ScrollView
below API 23 required subclassing ScrollView
or messing around with the ViewTreeObserver
of the ScrollView
which often means even more work than subclassing. With NestedScrollView
it can be done using the build-in setter。
除了给出的答案中列出的优点之外,NestedScrollView 相对于 ScrollView 的另一优点是它与 CoordinatorLayout 的兼容性。 ScrollView 不与 CoordinatorLayout 配合。您必须使用 NestedScrollView 来获得工具栏的 "scroll off-screen" 行为。
NestedScrollView 就像 ScrollView,但是在 NestedScrollView 中我们可以将其他滚动视图作为它的子视图,例如RecyclerView.
但是如果我们把RecyclerView放在NestedScrollView里面,RecyclerView的平滑滚动就会被打乱。所以要恢复平滑滚动有一个技巧:
ViewCompat.setNestedScrollingEnabled(recyclerView, false);
为 recyclerView 设置适配器后放在上面。
我认为使用嵌套滚动视图的一个好处是协调器布局
只监听嵌套的滚动事件。所以如果对于前。当您滚动 activity 的内容时,您希望工具栏向下滚动,只有当您在布局中使用嵌套滚动视图时,它才会向下滚动。如果您在布局中使用普通滚动视图,则当用户滚动内容时工具栏不会滚动。
ScrollView
和 NestedScrollView
有什么区别?他们两个,扩展FrameLayout
。我想深入了解两者的优缺点。
NestedScrollView
顾名思义,当需要在另一个滚动视图中使用滚动视图时使用。通常这很难完成,因为系统无法决定滚动哪个视图。
这就是 NestedScrollView
的用武之地。
NestedScrollView
NestedScrollView is just like ScrollView, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.
https://developer.android.com/reference/android/support/v4/widget/NestedScrollView.html
ScrollView
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display. A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects
https://developer.android.com/reference/android/widget/ScrollView.html
除了nested scrolling NestedScrollView
added one major functionality, which could even make it interesting outside of nested contexts: It has build in support for OnScrollChangeListener
. Adding a OnScrollChangeListener
to the original ScrollView
below API 23 required subclassing ScrollView
or messing around with the ViewTreeObserver
of the ScrollView
which often means even more work than subclassing. With NestedScrollView
it can be done using the build-in setter。
除了给出的答案中列出的优点之外,NestedScrollView 相对于 ScrollView 的另一优点是它与 CoordinatorLayout 的兼容性。 ScrollView 不与 CoordinatorLayout 配合。您必须使用 NestedScrollView 来获得工具栏的 "scroll off-screen" 行为。
NestedScrollView 就像 ScrollView,但是在 NestedScrollView 中我们可以将其他滚动视图作为它的子视图,例如RecyclerView.
但是如果我们把RecyclerView放在NestedScrollView里面,RecyclerView的平滑滚动就会被打乱。所以要恢复平滑滚动有一个技巧:
ViewCompat.setNestedScrollingEnabled(recyclerView, false);
为 recyclerView 设置适配器后放在上面。
我认为使用嵌套滚动视图的一个好处是协调器布局 只监听嵌套的滚动事件。所以如果对于前。当您滚动 activity 的内容时,您希望工具栏向下滚动,只有当您在布局中使用嵌套滚动视图时,它才会向下滚动。如果您在布局中使用普通滚动视图,则当用户滚动内容时工具栏不会滚动。