My activity has a NavigationView
inside a DrawerLayout
.
When user clicks on a button in the NavigationView header, I would like to scroll to top the DrawerLayout/NavigationView
with an animation.
It seems that NavigationView and DrawerLayout don't provide a method to get the actual scroll position (getScrollY()
and getScrollX()
always return 0) and so I can't do that.
How can I scroll to top with an animation?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- My content -->
<android.support.design.widget.NavigationView
android:id="@+id/navigation"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/navigation_view_header"
app:menu="@menu/my_navigation_items" />
</android.support.v4.widget.DrawerLayout>
you need to scroll the NavigationMenuView.
In the below code, it will scroll to top when click on any item in menu
NavigationView navigationView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
//here will scroll to top
((NavigationMenuView)navigationView.getChildAt(0)).smoothScrollToPosition(0);
return true;
}
EDIT: If you want to protect against possible changes down the road, you can use reflection here, like so:
// try to scroll to the top of the navigation menu, if possible
if (navigationView.getChildCount() > 0) {
View childView = navigationView.getChildAt(0);
try {
Method scroll = childView.getClass().getMethod("smoothScrollToPosition", int.class);
scroll.invoke(childView, 0);
} catch (NoSuchMethodException |
SecurityException |
IllegalAccessException |
InvocationTargetException e) {
Log.d(TAG, "smoothScrollToPosition", e);
}
}