以编程方式滚动到NestedScrollView的顶部

96
有没有一种以编程方式滚动到NestedScrollView顶部并触发父级滚动事件的方法?smoothScrollTo(x, y)不会委托滚动事件给NestedScrollingParent

请进一步澄清您的问题,您是指当NestedScrollView位于协调布局内时吗? - Pier Betos
要编程滚动到任何点,请查看 https://dev59.com/vSP-s4cB2Jgan1znaKdi - user1506104
14个回答

128
NestedScrollView.scrollTo(0, 0);

7
我建议使用NestedScrollView.scrollTo(0, 0);NestedScrollView滚动到顶部。不客气。 - SilentKnight
这通常适用于ScrollView或NestedScrollView。 - Robert
@Override public void onStart() { super.onStart(); scroll.post(() -> scroll.scrollTo(0, 0)); } - Guilherme Simão Couto
在C# Xamarin中可以使用MyScroller.ScrollTo(0, 0);这样的语句。 - cherry

44

我成功地做到了(动画滚动):

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

其中10000表示y方向上的速度。


1
如果您只想像我一样通过滚动来隐藏appbarlayout,则可以执行behavior.onNestedFling(coordinator, appbar, null, 0,-params.height, true);。要取消隐藏,请执行behavior.onNestedFling(coordinator, appbar, null, 0, params.height, true); - Kosh
3
有办法让嵌套滚动视图滚动到底部吗? - Bibu
当AppBarLayout扩展时,模拟嵌套的fling将使其折叠(非常好),但不会同时使NestedScrollView向下滚动... - Raphael Royer-Rivard
此外,当AppBarLayout完全折叠时,使用负y速度值将无法展开它。 - Raphael Royer-Rivard

38

另一种平滑滚动 NestedScrollView 到顶部或底部的方法是使用 fullScroll(direct) 函数。

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

fullScroll(View.FOCUS_UP)隐藏的应用栏没有出现。有什么想法吗? - Shabbir Dhangot
@Sabbir Dhangot,你有没有将你的NestedScrollView布局放在应用栏下面? - MeLean
我有一个问题,我可以看到fullScroll接收一个视图。如果我想滚动到某个TextView、EditText或RecyclerView,它会滚动到它吗? - Felipe Franco

24

对我而言,一切都行不通,我真的不知道为什么这个方法管用,但这是我的解决方案和问题。

当将RecyclerView添加到嵌套的ScrollView中时,在进入该活动时会在屏幕上显示RecyclerView。为了滚动到顶部,我必须使用以下代码:

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

5
哇塞,这太棒了,该死的Garm。 - abbasalim
11
如果有人的问题是在进入活动时,嵌套滚动视图中的RecyclerView已经滚动了一半,请尝试将XML根下最顶层的元素设置为android:focusableInTouchMode="true"。 - user1025013
1
这个解决方案完美地运行,而 scrollView.scrollTo(0,0); 则根本不起作用。 - taranjeetsapra
4
这个代码可以起作用:scrollView.getParent().requestChildFocus(scrollView, scrollView); - Rajeev Sahu

23

我也遇到过类似的情况。在我的情况下,当我向下滚动到底部时,FAB按钮应该出现,当用户点击该FAB按钮时,应该返回到页面顶部。 为此,我添加了@SilentKnight的答案 NestedScrollView.scrollTo(0, 0); 去返回到顶部,但这不足以实现平滑的向上滚动动画。
为了实现平滑的动画效果,我使用了@Sharj的答案,即 NestedScrollView.fullScroll(View.FOCUS_UP);。 但是,这样我的AppBar就不可见了,因此我必须扩展AppBar,如下所示:appBarLayout1.setExpanded(true)。 因此,使用这三种方法,我可以平滑地返回到页面顶部。

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);
appBarLayout1.setExpanded(true);

6
appBarLayout1.setExpanded(true); - 对我有效!谢谢 :) (翻译:这段代码对作者产生了作用,感谢!) - Andrei
谢谢!这是最正确的答案,特别是在使用AppBarLayout时。 - demogorgorn

16

有时候 NestedScrollView.scrollTo(0, 0);scrollview.pageScroll(View.FOCUS_UP); 不起作用。

这时候需要同时使用 fling()smoothScrollTo()

示例代码

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

15

您可以使用此功能实现平滑滚动。

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

普通滚动的OR

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

干得好。你节省了我的时间。 - Priyanka G

11

您可以轻松地在NestedScrollView层上伪造滚动。基本上,您要告诉coordinatorLayout,您刚刚滚动了工具栏的高度。

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

对我有用的唯一答案!我需要在协调布局中向上滚动(因此嵌套滚动视图和可折叠工具栏都要滚动)。我使用了现有的代码,但是将toolbarHeight更改为Integer.MIN_VALUE,-toolbarHeight更改为Integer.MAX_VALUE(因为我需要相反的方向)。然后我调用appBarLayout.setExpanded(true,true); 谢谢! - Viktoriia Chebotar

7

我尝试了以上所有方法,似乎都不起作用,但我只需要一个postDelayed。

    scrollview.postDelayed(new Runnable() {
        @Override
        public void run() {
            listener.setAppBarExpanded(false, true); //appbar.setExpanded(expanded, animated);
            scrollview.fullScroll(View.FOCUS_DOWN);
        }
    }, 400);

4

将这行代码重复两次。 对我有用。

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接