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

luk*_*994 68 android android-layout android-nestedscrollview

有没有办法以编程方式滚动到顶部NestedScrollView还通过触发父级的滚动事件?smoothScrollTo(x, y)不会将滚动事件委托给NestedScrollingParent.

Sil*_*ght 91

NestedScrollView.scrollTo(0, 0);
Run Code Online (Sandbox Code Playgroud)

  • 我告诉你使用`NestedScrollView.scrollTo(0,0);`滚动到`NestedScrollView`的顶部.别客气. (7认同)
  • 这怎么不是公认的答案? (2认同)

luk*_*994 42

我设法做到了(动画滚动):

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);
}
Run Code Online (Sandbox Code Playgroud)

10000y方向的速度在哪里.

  • 谢谢.有关如何滚动到nestedscrollview底部的任何想法? (3认同)

Sha*_*arj 30

平滑滚动NestedScrollView到上下的另一种方法是使用fullScroll(direct)函数

nestedScrollView.fullScroll(View.FOCUS_DOWN);
nestedScrollView.fullScroll(View.FOCUS_UP);
Run Code Online (Sandbox Code Playgroud)


Vul*_*sin 21

没有什么对我有用,我不知道为什么会这样,但这是我的解决方案和问题.

将循环器视图添加到嵌套滚动视图时,它会在进入该活动时在屏幕上显示回收器视图.为了一直向上滚动,我不得不使用这个:

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);
Run Code Online (Sandbox Code Playgroud)

  • 如果任何人的问题是嵌套滚动视图中的recyclerview在进入活动时已经中途滚动,请尝试在XML的根目录下设置最顶层的元素以具有以下属性android:focusableInTouchMode ="true" (8认同)
  • 哇,这是最好的,damet Garm (4认同)
  • 这工作:`scrollView.getParent().requestChildFocus(scrollView,scrollView);` (3认同)

小智 16

我也遇到了类似的情况.在我的情况下,当我向下滚动到结尾时,应该出现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);
Run Code Online (Sandbox Code Playgroud)

  • appBarLayout1.setExpanded(真); - 为我工作!谢谢 :) (4认同)

use*_*201 9

您可以轻松伪造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());
Run Code Online (Sandbox Code Playgroud)


Ali*_*ram 8

您可以使用它进行平滑滚动。

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.smoothScrollTo(0,0);
Run Code Online (Sandbox Code Playgroud)

或正常滚动

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.scrollTo(0,0);
Run Code Online (Sandbox Code Playgroud)


Nil*_*hod 8

有的时候NestedScrollView.scrollTo(0, 0);scrollview.pageScroll(View.FOCUS_UP);不能正常工作

在那你需要使用 fling()smoothScrollTo()一起

示例代码

nestedScrollView.post {
   nestedScrollView.fling(0)
   nestedScrollView.smoothScrollTo(0, 0)
}
Run Code Online (Sandbox Code Playgroud)


Mon*_*ska 5

我尝试了上述所有响应,但似乎没什么用,但我只需要一个postDelayed。

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