Auto Scroll for Specific View

Asked

Viewed 58 times

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 make an auto scroll so that the application shows on the screen that element I want, in case, the beginning of a Linearlayout.

You can solve it with linearlayout.requestFocus(), but there is no scroll, there is some other way the application shows the scrool by sliding to the view I want?

Thank you.

1 answer

2


Try it like this:

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

If the Element is not inside the vertical_scroll, the position of the parent should be taken into account:

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

EDIT:

To smooth the 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!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.