NestedScrollView's fullScroll(View.FOCUS_UP) not working properly

后端 未结 4 1604
面向向阳花
面向向阳花 2020-12-10 05:12

I have a NestedScrollView populated with a vertical LinearLayout, which itself has a bunch of children of various view types: multiple TextViews, two static GridViews, and e

相关标签:
4条回答
  • 2020-12-10 05:42

    It seems to it's finally fixed in 28.0.0 Support lib, so NestedScrollView.smoothScrollTo() works as expected for me

    0 讨论(0)
  • 2020-12-10 05:50

    This code works for me.

    scrollView.post {
        scrollView.fling(0)
        scrollView.fullScroll(ScrollView.FOCUS_UP)
    }
    
    0 讨论(0)
  • 2020-12-10 05:53

    UPDATE:

    Found a better solution. Try this combo:

    scrollView.fling(0);  // Sets mLastScrollerY for next command
    scrollView.smoothScrollTo(0, 0);  // Starts a scroll itself
    

    Looks like a hack, but works well on both my devices (tested with Support Library 26.0.0 & 27.0.2). Can be used to scroll smoothly to any other position. Based on the idea that the problem lies in missing update of mLastScrollerY.

    Please leave a feedback if it works for you or not.

    Original answer:

    Hit this issue too.

    NestedScrollView.smoothScrollTo(0, 0) worked in Support Library up to 25.4.0, and doesn't work since 26.0.0 up to current 27.0.2. The contents of NestedScrollView doesn't matter (some TextViews are enough).

    This bug is already reported twice on Google Issue Tracker. Minimal project to reproduce it can be found on GitHub.

    Found two working solutions:

    1. NestedScrollView.scrollTo(0, 0) (see accepted answer, jumps abruptly)
    2. NestedScrollView.fling(-10000) (scrolls smoothly, but appropriate velocity value depends on current position, device, desired scrolling time and so on)

    It was not necessary to change focusability in my case.

    0 讨论(0)
  • 2020-12-10 06:00

    try this

    add android:descendantFocusability="blocksDescendants" to the LinearLayout inside NestedScrollView and this also

    to scroll to top of NestedScrollView use this

    NestedScrollView.scrollTo(0, 0);
    

    Edit

    Use fling() and smoothScrollTo togather

    nestedScrollView.post(new Runnable() {
       @Override
       public void run() {
          nestedScrollView.fling(0);
          nestedScrollView.smoothScrollTo(0, 0);
       }
    });
    
    0 讨论(0)
提交回复
热议问题