Programmatically scroll to the top of a NestedScrollView

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

    I had problem with NestedScrollView when I used than with RecyclerView . This code worked for me: 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);
    
    0 讨论(0)
  • 2020-12-13 12:18

    Add the line two times .worked for me

    nestedScrollView.fullScroll(View.FOCUS_UP);
    nestedScrollView.fullScroll(View.FOCUS_UP);
    
    0 讨论(0)
  • 2020-12-13 12:23

    Nothing worked for me, and I don't really know why this worked, but here is my solution and problem.

    When adding recycler view to a nested scroll view, it showed recycler view on screen when getting to that activity. In order to scroll all the way up, I had to use this:

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

    Some time NestedScrollView.scrollTo(0, 0); and scrollview.pageScroll(View.FOCUS_UP); does not work

    At that you need to use fling() and smoothScrollTo() together

    SAMPLE CODE

    nestedScrollView.post {
       nestedScrollView.fling(0)
       nestedScrollView.smoothScrollTo(0, 0)
    }
    
    0 讨论(0)
  • 2020-12-13 12:29

    I managed to do it (animated scrolling):

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

    where 10000 is the velocity in y-direction.

    0 讨论(0)
提交回复
热议问题