Programmatically scroll to the top of a NestedScrollView

AndroidAndroid LayoutAndroid Nestedscrollview

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

NestedScrollView.scrollTo(0, 0);

Solution 2 - Android

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.

Solution 3 - Android

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);

Solution 4 - Android

Nothing worked for me, and I don't really know why this worked, but here is my solution and problem.

When adding recycler view to a nested scroll view, it showed recycler view on screen when getting to that activity. In order to scroll all the way up, I had to use this:

RecyclerView recyclerView = (RecyclerView) findViewById(R.id.details_recycler_view);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setItemAnimator(new DefaultItemAnimator());
ProductDescriptionAdapter adapter = new ProductDescriptionAdapter(this);
adapter.setData(mProduct.getDetails());
recyclerView.setAdapter(adapter);
NestedScrollView scrollView = (NestedScrollView) findViewById(R.id.scroll);
scrollView.getParent().requestChildFocus(scrollView, scrollView);

Solution 5 - Android

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);

Solution 6 - Android

Some time NestedScrollView.scrollTo(0, 0); and scrollview.pageScroll(View.FOCUS_UP); does not work

At that you need to use fling() and smoothScrollTo() together

SAMPLE CODE

nestedScrollView.post {
   nestedScrollView.fling(0)
   nestedScrollView.smoothScrollTo(0, 0)
}

Solution 7 - Android

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);

Solution 8 - Android

You could easily fake the scrolling on NestedScrollView level. You basically tell the coordinatorLayout that you just scrolled by the height of the toolbar.

    NestedScrollView nestedScrollView = (NestedScrollView) getActivity().findViewById(R.id.your_nested_scroll_view);

    int toolbarHeight = getActivity().findViewById(R.id.toolbar).getHeight();

    nestedScrollView.startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL);
    nestedScrollView.dispatchNestedPreScroll(0, toolbarHeight, null, null);
    nestedScrollView.dispatchNestedScroll(0, 0, 0, 0, new int[]{0, -toolbarHeight});
    nestedScrollView.scrollTo(0, nestedScrollView.getBottom());

Solution 9 - Android

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);

Solution 10 - Android

Add the line two times .worked for me

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.fullScroll(View.FOCUS_UP);

Solution 11 - Android

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

findViewById({your NestedScrollView resource id}).scrollTo(0, 0);

Solution 12 - Android

I had problem with NestedScrollView when I used than with RecyclerView . This code worked for me: nestedScrollView!!.getParent().requestChildFocus(nestedScrollView, nestedScrollView);

    val recyclerViewSearch = activity?.findViewById<RecyclerView>(R.id.recycler)
    recyclerViewSearch.setAdapter(setAdapterSearch())

    val nestedScrollView = activity?.findViewById<NestedScrollView>(R.id.bottom_sheet_Search)
    nestedScrollView!!.getParent().requestChildFocus(nestedScrollView, nestedScrollView);
    

Solution 13 - Android

Here is how I scroll to an item of RecyclerView when RecyclerView is inside NestedScrollView

First, get an item height then scroll to the position that you want.

val itemHeight =
    binding.calendarRecyclerView.findViewHolderForAdapterPosition(0)!!.itemView.height
binding.nestedScrollView2.smoothScrollTo(0, position * itemHeight)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionlukas1994View Question on Stackoverflow
Solution 1 - AndroidSilentKnightView Answer on Stackoverflow
Solution 2 - Androidlukas1994View Answer on Stackoverflow
Solution 3 - AndroidSharjeelView Answer on Stackoverflow
Solution 4 - AndroidVulovic VukasinView Answer on Stackoverflow
Solution 5 - AndroidViduView Answer on Stackoverflow
Solution 6 - AndroidAskNileshView Answer on Stackoverflow
Solution 7 - AndroidAli AkramView Answer on Stackoverflow
Solution 8 - Androiduser1259201View Answer on Stackoverflow
Solution 9 - AndroidMontDeskaView Answer on Stackoverflow
Solution 10 - AndroidSaljith KjView Answer on Stackoverflow
Solution 11 - AndroidSokdara ChengView Answer on Stackoverflow
Solution 12 - AndroidMaryam AzhdariView Answer on Stackoverflow
Solution 13 - AndroidAbdurahman PopalView Answer on Stackoverflow