androiddrawerlayoutnavigationview

Reset NavigationView scroll


simple question which I haven't been able to find an answer to or figure out by myself.

How can I have the NavigationView always start on its "original" scroll position whenever it's open, so the first menu item is always visible regardless of where the scroll position was left previously.

I've tried this without success:

drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
    ...

    @Override
    public void onDrawerClosed(View drawerView) {
        drawerView.scrollTo(0, 0);
    }

    ...
});

Thanks.


Solution

  • Well after examining the View hierarchy of the NavigationView widget, it's comprised of a NavigationMenuView that extends RecyclerView, so using this I've come up with this shenanigan:

    ((NavigationMenuView)((NavigationView)drawerView).getChildAt(0)).scrollToPosition(0);