How to properly scroll to a position in nestedscrollview which is inside a coordinate layout? When I use scrollTo(), it seems like the view inside AppBarLayout is loosing the ability to scroll down when I scroll to the end of NestedScrollView and coming back to top. What I need to know is how to programtically scroll to a position in nestedscrollview without causing problem with the normal behavior of collapsing layout.
Issue is similar to what is mentioned in this post:
The Coordinator layout doesn't Scroll up for a specific position
There is a Gif attached along with the above question:
https://i.stack.imgur.com/uSGfX.gif
I have tried the solution in above link, but it is not working.
Here is what I am trying to achieve:
Link to google's material design guide
In my case I have multiple cards in a scrollView, which contains a linear layout. I did not use a recycler view because the number of cards is always about the same (below 10). One of the cards contains two recyclerviews showing a list of for example comments. The problem is that I can't find a way to disable scrolling of the internal recyclerview without causing it to not scroll at all and show incomplete data.
Thanks for your help!
Use a NestedScrollView and put your RecyclerView inside it.
If you wish the RecyclerView to become a scrolling part of your NestedScrollView, set nested scrolling to false.
NestedScrollView nestedScrollView = findViewById(R.id.myNestedScrollView);
nestedScrollView.setNestedScrollingEnabled(false);
If you want an independent scroll you don't need to do anything since nested scrolling is set to true by default.
I have two NestedScrollView with a visibility switch in the BottomSheet. The problem is that BottomSheet scrolling works only for the first NestedScrollView in the sources.
I solved the problem now, divided one BottomSheet into two. And programmatically change FABs app:layout_anchor.
What is the difference between ScrollView and NestedScrollView? Both of them, extend FrameLayout. I want to know in depth pros and cons of both of them.
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.
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.
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
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
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.
I think one Benefit of using Nested Scroll view is that the cooridinator layout
only listens for nested scroll events. So if for ex. you want the toolbar to scroll down when you scroll you content of activity, it will only scroll down when you are using nested scroll view in your layout. If you use a normal scroll view in your layout, the toolbar wont scroll when the user scrolls the content.
<androidx.core.widget.NestedScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
// your Layout xml code
</androidx.core.widget.NestedScrollView>
I made collapsible toolbar on Activity, but there is one problem.
Screen contains RecyclerView that has multiple layout items, and one of them is another RecyclerView that has horizontal scroll. When main recycler is scrolled, toolbar doesn't expand or collapse when scroll started on that nested recycler, but on other views it's working as it should.
To make it easier to understand: when I first touch screen on position where there is nested RecyclerView and try to scroll up and down, there is no collapsing or expanding of toolbar.
Any idea what might be causing that issue and how to fix it?
I only needed to set recyclerView.setNestedScrollingEnabled(false) for the nested RecyclerView to work.
Thanks to Reddit user in this thread:
Toolbar not collapsing with nested RecyclerView
For those who would like the same thing in Kotlin is:
recyclerView.isNestedScrollingEnabled = false
And for XML it is this property in RecyclerView but only for API 21 and higher:
android:nestedScrollingEnabled="false"