Programmatically scroll to the top of a NestedScrollView
Asked Answered
N

14

96

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.

Nephrotomy answered 23/6, 2015 at 22:10 Comment(2)
Clarify your question more, do you mean to say when the NestedScrollView is inside the coordinator layout?Shorter
to programmatically scroll to any point, check #52084178Duhl
N
44

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.

Nephrotomy answered 4/7, 2015 at 6:58 Comment(4)
if you only like to scroll to hide the appbarlayout like in my case you could do behavior.onNestedFling(coordinator, appbar, null, 0, -params.height, true); to reverse it behavior.onNestedFling(coordinator, appbar, null, 0, params.height, true);Cavanagh
Thx. Any idea on how to scroll to the bottom of the nestedscrollview ?Typewriter
When the AppBarLayout is expanded, simulating the nested fling will collapse it (which is great), but will not make the NestedScrollView scroll down at the same time...Ordinate
Plus, when the AppBarLayout is fully collapsed, using a negative y velocity value will not expand it.Ordinate
H
128
NestedScrollView.scrollTo(0, 0);
Humber answered 10/7, 2015 at 5:46 Comment(4)
I am telling your to use NestedScrollView.scrollTo(0, 0); to scroll to the top of the NestedScrollView. You are welcome.Humber
This is more generally applied to both ScrollView or NestedScrollView.Nealneala
@Override public void onStart() { super.onStart(); scroll.post(() -> scroll.scrollTo(0, 0)); }Johnsson
Works in C# Xamarin like MyScroller.ScrollTo(0, 0);Clywd
N
44

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.

Nephrotomy answered 4/7, 2015 at 6:58 Comment(4)
if you only like to scroll to hide the appbarlayout like in my case you could do behavior.onNestedFling(coordinator, appbar, null, 0, -params.height, true); to reverse it behavior.onNestedFling(coordinator, appbar, null, 0, params.height, true);Cavanagh
Thx. Any idea on how to scroll to the bottom of the nestedscrollview ?Typewriter
When the AppBarLayout is expanded, simulating the nested fling will collapse it (which is great), but will not make the NestedScrollView scroll down at the same time...Ordinate
Plus, when the AppBarLayout is fully collapsed, using a negative y velocity value will not expand it.Ordinate
D
38

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);
Drexler answered 13/7, 2016 at 20:56 Comment(3)
fullScroll(View.FOCUS_UP) hidden appbar not coming out. Any IdeaLohner
@Sabbir Dhangot did you layout your NestedScrollView below the appbar?Game
I have one question I can see that fullScroll receives a view. if I want to scroll to a certain textview, editext, or recyclerView will it scroll to it?Retain
O
24

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);
Ouzo answered 3/9, 2016 at 9:55 Comment(4)
Wooooow, this is best, damet GarmSlotter
If anyone's problem is that the recyclerview inside the nested scroll view is already scrolled half way when entering the activity, try setting the top most element under root of XML to have the following property android:focusableInTouchMode="true"Opossum
This solution works perfectly where as scrollView.scrollTo(0,0); does not work at all.Annettannetta
This worked : scrollView.getParent().requestChildFocus(scrollView, scrollView);Benadryl
A
23

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);
Athiste answered 23/3, 2018 at 5:30 Comment(2)
appBarLayout1.setExpanded(true); - worked for me! Thanks :)Ford
Thank you! This is the most correct answer, especially when using appbarlayoutGlacis
D
17

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)
}
Dollie answered 14/12, 2019 at 7:36 Comment(0)
S
15

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);
Solifluction answered 23/7, 2018 at 6:53 Comment(1)
Gr8 job. You save my timeGuimpe
D
11

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());
Debark answered 21/1, 2016 at 9:58 Comment(1)
The only answer that worked for me! I needed to scroll up in coordinator layout (so both nestedScrollView and collapsibleToolbar). I used existing code but toolbarHeight changed to Integer.MIN_VALUE and -toolbarHeight to Integer.MAX_VALUE (as I need opposite dirrection). And then I call appBarLayout.setExpanded(true, true); Thank you!Ecumenicism
V
7

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);
Vaticide answered 26/9, 2018 at 10:38 Comment(0)
H
4

Add the line two times .worked for me

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.fullScroll(View.FOCUS_UP);
Harberd answered 10/4, 2019 at 6:54 Comment(0)
F
1

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

findViewById({your NestedScrollView resource id}).scrollTo(0, 0);
Fluctuant answered 9/2, 2018 at 11:5 Comment(0)
C
0

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);
Couch answered 9/12, 2018 at 21:7 Comment(0)
H
0

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)
Haberdashery answered 3/12, 2019 at 9:51 Comment(0)
K
0

Kotlin:

scrollView.parent.requestChildFocus(scrollView, scrollView)
Kitts answered 19/2, 2023 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.