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"