Android: ScrollView vs NestedScrollView
Solution 1
NestedScrollView
as the name suggests is used when there is a need for a scrolling view inside another scrolling view. Normally this would be difficult to accomplish since the system would be unable to decide which view to scroll.
This is where NestedScrollView
comes in.
Solution 2
In addition to the 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.
Solution 3
Other than the advantages listed in the answers given, one more advantage of NestedScrollView over ScrollView is its compatibility with CoordinatorLayout. The ScrollView does not cooperate with the CoordinatorLayout. You have to use NestedScrollView to get "scroll off-screen" behaviour for the toolbar.
Toolbar will not collapse with Scrollview as child of CoordinatorLayout
Solution 4
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
Solution 5
NestedScrollView is just like ScrollView, but in NestedScrollView we can put other scrolling views as child of it, e.g. RecyclerView.
But if we put RecyclerView inside NestedScrollView, RecyclerView's smooth scrolling is disturbed. So to bring back smooth scrolling there's trick:
ViewCompat.setNestedScrollingEnabled(recyclerView, false);
put above line after setting adapter for recyclerView.
Related videos on Youtube
Chintan Soni
Hey visitor, Greetings !!! I am Chintan Soni, a Passionate, Tech savvy, Independent, Self-motivating Mobile Application Developer. Currently serving as TechLead - Android @ ARInspect. Always eager to learn new technologies. Love to help people learn with, whatever I got. Meet my brother: Yash Soni A tech-enthusiast, Gadget Freak and Apple Fanboy. Motto that LIT my life: Learn, Implement and Teach others. Github: https://github.com/iChintanSoni Medium: https://medium.com/@chintansoni My Mentor: Paresh Mayani, Pratik Patel Currently onto: Flutter Kotlin Platforms and Frameworks I have worked on: Android Native Application Development Ionic 3.x Application Development Microsoft ASP.net Web Api 2 using Entity Framework 6.1 (MSSQL Server Database 2014) Node.js (MySQL Server) Google Firebase Augmented Reality Facebook Messenger Bots And what not...!!! Follow me on: Facebook, Twitter, LinkedIn
Updated on August 25, 2020Comments
-
Chintan Soni over 3 years
What is the difference between
ScrollView
andNestedScrollView
? Both of them, extendFrameLayout
. I want to know in depth pros and cons of both of them. -
AdamHurwitz almost 8 yearsHere's a sample App demonstrating a NestedScrollView within a parent ScrollView: github.com/AdamSHurwitz/NestedScrolling
-
Martin Marconcini almost 4 yearsKeep in mind that doing this (recyclerView inside a Nested) is quite inefficient in terms of layout passes/measures. Test it and you will see how the RV stops "recycling" views.