lukas1994 lukas1994 - 4 months ago 498
Android Question

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
.

Answer

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.

Comments