Programmatically scroll to the top of a NestedScrollView

后端 未结 13 1642
情书的邮戳
情书的邮戳 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:27

    You could easily fake the scrolling on NestedScrollView level. You basically tell the coordinatorLayout that you just scrolled by 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());
    

提交回复
热议问题