Search code examples
androidnestedscrollview

Smoothy scroll to Child in NestedScrollView


I have expandable views inside CardView thats parent is NestedScrollView. I'm trying to create smooth scroll to child when expand animation ended. But I found only one solution:

 scrollView.requestChildFocus(someView, someView);

This code works fine, but, when call requestChildFocus it scrolls immediately, and that annoying me a little bit. Is it possible to scroll to child smoothly?


Solution

  • The childView, to which I wanted to scroll, has CardView parrent, so childView.getTop() returns the value relative to the CardView not to the ScrollView. So, to get top relative to ScrollView I should get childView.getParent().getParent() then cast it to View and call getTop().

    Scroll position calculates like

    int scrollTo = ((View) childView.getParent().getParent()).getTop() + childView.getTop();
    nestedScrollView.smoothScrollTo(0, scrollTo);