1. Android control ScrollView to scroll to the bottom
In development, we often need to update the list and pull the list to the bottom, such as posting on Weibo, chat interface, etc.
There are two ways here. The first is to use scrollTo():
public static void scrollToBottom(final View scroll, final View inner) { Handler mHandler = new Handler(); (new Runnable() { public void run() { if (scroll == null || inner == null) { return; } int offset = () - (); if (offset < 0) { offset = 0; } (0, offset); } }); }
The first implementation is relatively troublesome, and it is recommended to use the second method, using fullScrol()
Let's take a look at this function below:
-
(ScrollView.FOCUS_DOWN);
Scroll to the bottom -
(ScrollView.FOCUS_UP);
Scroll to top
What should be noted is:This method cannot be called directly
Because many Android functions are synchronized based on message queues, an operation is required. After the addView is finished, it does not mean that it will be displayed immediately, but is waiting for processing in the queue. Although it is very fast, if fullScroll is called immediately, the view may not be displayed yet, so it will fail
It should be updated in a new thread through handler
(new Runnable() { @Override public void run() { (ScrollView.FOCUS_DOWN); } });
2. ScrollView is prohibited from automatically sliding to the bottom
But sometimes we need to prohibit the ScrollView from automatically sliding to the bottom. Here is the solution:
Specific performance
When ScrollView nests GridView, ListView and other similar controls, refresh the interface when data is obtained from the network. What happens is: ScrollView automatically slides to the lowest end of the screen. Specifically, when sliding to display the last line of data, if you pull down and refresh at this time, the layout display will also occur.
how so
childView has the ability to get focus
This problem is caused because the childView exceeds the screen size and has the ability to get focus. Since it cannot be changed in size, it can only be prevented from gaining focus. The basic idea is to cancel its ability to get focus, let ScrovView intercept its focus, etc.
How to solve it
Let the childView focus be intercepted
Specific plan
LinearLayout under ScrollView has been addedandroid:descendantFocusability="blocksDescendants"
Summarize
The above is the entire content of this article. I hope that the content of this article has a certain reference value for everyone's study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.