Implementation method of Android Scrollview sliding to the top or specified position

In Android development, we often encounter the phenomenon that one screen can not display all the contents. We also know that we will think of Scrollview for scrolling display at this time.

At this time, due to some requirements, it is required to locate a certain location when the Scrollview is displayed at the beginning. This article is about this

First, Scrollview. Scrollto (x, y); This method can locate the scroll bar, which we all know.

But, it seems that many times the call of this method has no effect

It seems that calling the scrollto method mentioned above does not have the effect of positioning the scroll bar. In fact, it is because we call this method in oncreate. When the oncreate method does not end, the activity data is not fully loaded, so calling this method at this time will not have any actual effect.

So, the correct call method is coming ~

First define a runnable interface

Then use a handler to post the runnable object where you want to actively locate the position of the scroll bar. It's best to delay it

In this way, you can actively locate the Scrollview to any position ~

In addition, in some cases, you need to directly locate the Scrollview to the bottom display. You can use the following methods

Among them, LinearLayout is the sub view in the Scrollview, so you can directly display the bottom of the Scrollview when displaying

Scrollview scrolls to the specified position

Roll to the bottom:

mRootScrollView.fullScroll(ScrollView.FOCUS_DOWN);

Scroll to the top:

mRootScrollView.fullScroll(ScrollView.FOCUS_UP);

Note that you need to post a runnable to perform scrolling in the message queue!

First, handler. Post (runnable); It's not a new thread, it just lets the UI main thread execute the run () method concurrently.

Second, it is placed in the handler to ensure that the views have been drawn. Otherwise, you can execute it in resume().

Third, smoothscrollto is similar to scrollto, but scrolling is gentle rather than rolling to a certain place immediately. In addition, the smoothscrollto () method can break the sliding animation.

The above implementation method of Android Scrollview sliding to the top or specified position is all the content shared by Xiaobian. I hope it can give you a reference and support more programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>