Programmatically scroll up to the top of the NestedScrollView

Is there a way to programmatically scroll to the top of the NestedScrollView , while also raising scroll events for the parent? smoothScrollTo(x, y) does not pass scroll events to NestedScrollingParent .

+54
android android-layout android-nestedscrollview
Jun 23 '15 at
source share
10 answers

I succeeded (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 speed in the y direction.

+40
Jul 04 '15 at 6:58
source share
 NestedScrollView.scrollTo(0, 0); 
+72
Jul 10 '15 at 5:46
source share

Another way to smooth NestedScrollView scrolling all the way up or down is using the fullScroll(direct) function

 nestedScrollView.fullScroll(View.FOCUS_DOWN); nestedScrollView.fullScroll(View.FOCUS_UP); 
+28
Jul 13 '16 at 20:56
source share

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

When adding the recycler view to the nested scroll view, it showed the recycler view on the screen when this activity was reached. To scroll all the way, 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); 
+15
Sep 03 '16 at 9:55 on
source share

I also came across a similar kind of script. In my case, when I scroll down to the end, the FAB button should appear, and when the user clicks on this FAB button, it goes to the top of the page. For this, I added @SilentKnight answer NestedScrollView.scrollTo(0, 0); To go up, but this is not enough for smooth animation to scroll up.
For smooth animation, I used @Sharj's answer, which is NestedScrollView.fullScroll(View.FOCUS_UP); But then my AppBar is not visible there, so I need to extend the AppBar as follows appBarLayout1.setExpanded(true) . Thus, using these three, I can smoothly jump to the top of the page.

 nestedScrollView.fullScroll(View.FOCUS_UP); nestedScrollView.scrollTo(0,0); appBarLayout1.setExpanded(true); 
+10
Mar 23 '18 at 5:30
source share

You can easily fake scrolling at the NestedScrollView level. You basically tell the Layout coordinator that you just scrolled 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()); 
+8
Jan 21 '16 at 9:58 on
source share

You can use this to scroll smoothly.

 nestedScrollView.fullScroll(View.FOCUS_UP); nestedScrollView.smoothScrollTo(0,0); 

OR for normal scrolling

 nestedScrollView.fullScroll(View.FOCUS_UP); nestedScrollView.scrollTo(0,0); 
+3
Jul 23 '18 at 6:53
source share

Instead, use View.scollTo (int x, int y) to jump to the beginning.

 findViewById({your NestedScrollView resource id}).scrollTo(0, 0); 
+1
Feb 09 '18 at 11:05
source share

I tried all of the above answers, nothing seemed to work, but I only needed postDelayed.

  scrollview.postDelayed(new Runnable() { @Override public void run() { listener.setAppBarExpanded(false, true); //appbar.setExpanded(expanded, animated); scrollview.fullScroll(View.FOCUS_DOWN); } }, 400); 
0
Sep 26 '18 at 10:38
source share

I had a 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); 
0
Dec 09 '18 at 21:07
source share



All Articles