Is there a way to programmatically scroll to the top of a NestedScrollView
by also triggering the scroll events for the parent? smoothScrollTo(x, y)
NestedScrollView.scrollTo(0, 0);
You can use this for smooth scroll.
nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.smoothScrollTo(0,0);
OR for normal scroll
nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);
Use the View.scollTo(int x, int y) instead to scroll to the top.
findViewById({your NestedScrollView resource id}).scrollTo(0, 0);
Another way to smooth scroll NestedScrollView
to all the way up or down is using fullScroll(direct)
function
nestedScrollView.fullScroll(View.FOCUS_DOWN);
nestedScrollView.fullScroll(View.FOCUS_UP);
I tried all the above responses, nothin seemed to work but I only needed a postDelayed.
scrollview.postDelayed(new Runnable() {
@Override
public void run() {
listener.setAppBarExpanded(false, true); //appbar.setExpanded(expanded, animated);
scrollview.fullScroll(View.FOCUS_DOWN);
}
}, 400);
I also faced similar kind scenario. In my case when I scroll down to end, FAB button should be appears and when user tap on that FAB button should go to the top of the page.
For that I added @SilentKnight answer NestedScrollView.scrollTo(0, 0);
For go to the top but which is not enough for smooth animation for scroll up.
For smooth animation I have used @Sharj answer which is NestedScrollView.fullScroll(View.FOCUS_UP);
But then my AppBar is not visible there fore I have to expanded the AppBar as following appBarLayout1.setExpanded(true)
.
So using these three I can able smoothly go to top of the page.
nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);
appBarLayout1.setExpanded(true);