Programmatically scroll to the top of a NestedScrollView

后端 未结 13 1594
情书的邮戳
情书的邮戳 2020-12-13 12:02

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)

相关标签:
13条回答
  • 2020-12-13 12:30
    NestedScrollView.scrollTo(0, 0);
    
    0 讨论(0)
  • 2020-12-13 12:31

    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);
    
    0 讨论(0)
  • 2020-12-13 12:33

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

    findViewById({your NestedScrollView resource id}).scrollTo(0, 0);
    
    0 讨论(0)
  • 2020-12-13 12:34

    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);
    
    0 讨论(0)
  • 2020-12-13 12:34

    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);
    
    0 讨论(0)
  • 2020-12-13 12:36

    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);
    
    0 讨论(0)
提交回复
热议问题