Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically scroll to the top of a NestedScrollView

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) doesn't delegate the scroll events to the NestedScrollingParent.

like image 889
lukas1994 Avatar asked Jun 23 '15 22:06

lukas1994


People also ask

How do I scroll to top programmatically?

Use the View. scollTo(int x, int y) instead to scroll to the top.

What is nested scroll view?

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.

How do I prevent NestedScrollView from scrolling if body is small?

The problem can be solved by moving the SliverAppBar into the CustomScrollView and not use the NestedScrollView at all.


2 Answers

NestedScrollView.scrollTo(0, 0); 
like image 200
SilentKnight Avatar answered Oct 01 '22 04:10

SilentKnight


I managed to do it (animated scrolling):

CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) appBarLayout.getLayoutParams();
AppBarLayout.Behavior behavior = (AppBarLayout.Behavior) params.getBehavior();
if (behavior != null) {
    behavior.onNestedFling(coordinatorLayout, appBarLayout, null, 0, 10000, true);
}

where 10000 is the velocity in y-direction.

like image 21
lukas1994 Avatar answered Oct 01 '22 05:10

lukas1994