Smoothy scroll to Child in NestedScrollView

Near1999 picture Near1999 · Nov 4, 2015 · Viewed 12.4k times · Source

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?

Answer

Near1999 picture Near1999 · Nov 5, 2015

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