ScrollView automatically moves to the bottom when the display exceeds the current page

Source: Internet
Author: User

ScrollView refers to the view to be displayed in scrolling mode when there is a lot of content that cannot be displayed on one screen. For example, when creating a reader, the article is very long and the page cannot be displayed. You need to use the scroll view to scroll down the next page.
Java code
Private ScrollView mScrollView;
Private LinearLayout mLayout;
Private final Handler mHandler = new Handler ();
 
MScrollView = (ScrollView) findViewById (R. id. scroll );
MLayout = (LinearLayout) findViewById (R. id. linearlayout); // the outer layer of linearlayout is scroll.
MHandler. post (mScrollToBottom );
 
Private Runnable mScrollToBottom = new Runnable (){
@ Override
Public void run (){
// TODO Auto-generated method stub
Int off = mLayout. getMeasuredHeight ()-mScrollView. getHeight ();
If (off> 0 ){
MScrollView. scrollTo (0, off );
}
}
};

In Android, a single TextView cannot be rolled and must be placed in a ScrollView. ScrollView provides a series of functions, in which fullScroll is used to implement the home and end keys, that is, to scroll to the top and bottom.

However, if you call fullScroll immediately after the append of TextView, you will find that it cannot scroll to the real bottom. This is because many (if not all) functions in Android are message-based, message Queue is used to ensure synchronization. Therefore, most function calls are asynchronous operations. When TextView calls append, it does not wait for the text to be displayed, but adds the text to the message queue and returns immediately. When fullScroll is called, the text may not be displayed yet, naturally, you cannot scroll to the correct position.

The solution is actually very simple. Use post:
Java code
Final ScrollView svResult = (ScrollView) findViewById (R. id. svResult );
SvResult. post (new Runnable (){
Public void run (){
SvResult. fullScroll (ScrollView. FOCUS_DOWN );
}
});

 

Android moves ScrollView to the bottom
The scrollTo method can be used to adjust the display position of a view.
Call the following method as needed.
Scroll indicates the outer view, inner indicates the inner view, and other content is in inner.
Note that it is necessary to open a new thread in the method.
Otherwise, the getMeasuredHeight method is not the latest height when the data is updated.

Java code
Public static void scrollToBottom (final View scroll, final View inner ){
 
Handler mHandler = new Handler ();

MHandler. post (new Runnable (){
Public void run (){
If (scroll = null | inner = null ){
Return;
}
 
Int offset = inner. getMeasuredHeight ()-scroll. getHeight ();
If (offset <0 ){
Offset = 0;
}
 
Scroll. scrollTo (0, offset );
}
});
}

 

Author: "Shorts party"
 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.