Auto Scroll for Specific View

2

I have a ScrollView with several elements inside, LinearLayout, Images, Textviews etc.

In the header, above the scrollview I have some buttons, in the OnClickListener of one of them I want to do an auto scroll so that the application shows on the screen that element that I want, in this case, the start of a LinearLayout.

Thank you.

    
asked by anonymous 07.06.2016 / 19:09

1 answer

2

Try this:

vertical_scroll.scrollTo(0, exibirEste.getTop());

If the Element is not within vertical_scroll, consider the position of the parent:

int position = (exibirEste.getTop() + layoutPai.getTop());
vertical_scroll.scrollTo(0, position);

EDIT:

To smooth animation use:

int position = (exibirEste.getTop() + layoutPai.getTop());
ObjectAnimator.ofInt(vertical_scroll, "scrollY",  position).setDuration(1000).start(); 

Being, setDuration(1000) how long the animation should last!

    
07.06.2016 / 19:37