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

问题描述 投票:62回答:11

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

android android-layout android-nestedscrollview
11个回答
86
投票
NestedScrollView.scrollTo(0, 0);

1
投票

使用View.scollTo(int x,int y)代替滚动到顶部。

findViewById({your NestedScrollView resource id}).scrollTo(0, 0);

0
投票

我使用NestedScrollView时遇到问题而不是使用RecyclerView。这段代码对我有用: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);

41
投票

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

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方向的速度。


29
投票

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

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

20
投票

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

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

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

13
投票

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

8
投票

您可以轻松伪造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());

5
投票

您可以使用它来平滑滚动。

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

或者正常滚动

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

2
投票

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

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

2
投票

添加两行。为我工作

nestedScrollView.fullScroll(View.FOCUS_UP);
nestedScrollView.fullScroll(View.FOCUS_UP);
© www.soinside.com 2019 - 2024. All rights reserved.