Sometimes, we call the Scrollby function, the Scrollto function in the OnCreate function, there will be no effect of the situation
Public class showtraffic extends Activity
ScrollView Mscrollview = null;
@Override
protected void onCreate (Bundle savedinstancestate)
{
Super. OnCreate (savedinstancestate);
Setcontentview (r.layout.show);
Mscrollview = (ScrollView) Findviewbyid (R.id.show_scroll);
Mscrollview.postdelayed (NewRunnable ()
{
@Override
public void Run ()
{
intHeight = mlayout.getheight ();
intsum = MTraffics.mTraffics.size ();
Mscrollview.scrollto (0, (int) (Height/sum * index));
LOG.E ("", "Height:" + height + "sum:" + sum + "index:" + index);
}}, 300);
}
}
As I understand it, components such as ScrollView and LinearLayout are created at the time of OnCreate, and the height and width of the components are unknown at this time. Therefore, if you use the GetHeight () function directly to get the height of a component, its return value is 0. To be able to accurately get the height of the component, I think of the idea of using the Postdelayed method to defer execution of the code to get the height or set the scrolling value.
The layout is like this
In a rolling layout, a linear layout is included, and a linear layout contains a number of linear layouts, which in turn are buttons, text boxes, and so on.
Mscrollview (ScrollView)
|______________ mlayout(linearlayout)
|__________________itemlayout(linearlayout)
|__________________itemlayout(linearlayout)
|__________________itemlayout(linearlayout)
|_.......
- ScrollView provides scroll bars for some view that do not have scrollbars, by including the view that needs the scroll bar in <ScrollView>.
In the activity's OnCreate () method (which appears to be the same as in OnStart and Onresume), call Mscrollview.scrollto (0, 100); is invalid and has no effect. Looking for half a day, finally in http://stackoverflow.com/questions/3263259/ Scrollview-scrollto-not-working-saving-scrollview-position-on-rotation find the answer. (Google is better than Baidu Ah, unfortunately the snapshot can not be used)
ScrollTo () is the position of the scroll bar directly, but since this action is not purely about ScrollView, it is also based on the actual information of the view contained in the ScrollView. So this action must be done after the page is loaded.
To specify the scroll bar position during activity initialization, you must use the following code
[Java]View Plaincopy
- Mscrollview.post (new Runnable () {
- @Override
- public Void Run () {
- Mscrollview.scrollto (0, 1000);
- }
- });
Description of the Post () method:
Causes the Runnable to is added to the message queue. The runnable'll be run on the user interface thread.
This code really does play a role, The post () method is the view class, there seems to be a similar problem not only ScrollView exist!
In order to know what the reason, we start from the commissioning!
The first step is to use Mscrollview.scrollto directly (0, 1000); , debug found that during activity initialization, the ScrollTo () method has been executed, with F5, the internal execution of 17 steps.
The second step, using the post () method, debugging found that the activity initialization process, post () execution, activity initialization, after the ScrollTo () method is executed, with F5, the internal execution of 25 steps, of which the 18th step is Onscrollchang Ed (Mscrollx, Mscrolly, OLDX, OldY); However , the effect occurs after the message loop.
The third step, in the Ontouchevent method, executes the call Mscrollview.scrollto (0, 100); ScrollTo () method has been executed, with F5, debugging, internal steps a lot, absolutely more than 25, no patience in the back number. The effect is not clear where it appears.
I read the source code of ScrollView.
[Java]View Plaincopy
- /**
- * {@inheritDoc}
- *
- * <p>this version also clamps the scrolling to the bounds of our child.
- */
- @Override
- Public void ScrollTo (int x, int y) {
- //We rely on the fact the View.scrollby calls ScrollTo.
- if (Getchildcount () > 0) {
- View child = Getchildat (0);
- x = Clamp (x, GetWidth ()-Mpaddingright-mpaddingleft, Child.getwidth ());
- y = Clamp (y, getheight ()-Mpaddingbottom-mpaddingtop, Child.getheight ());
- if (x! = MSCROLLX | | Y! = mscrolly) {
- Super.scrollto (x, y);
- }
- }
- }
Its parent-Class View
[Java]View Plaincopy
- Public void ScrollTo (int x, int y) {
- if (mscrollx! = x | | mscrolly! = y) {
- int oldx = MSCROLLX;
- int oldY = mscrolly;
- MSCROLLX = x;
- mscrolly = y;
- Onscrollchanged (MSCROLLX, mscrolly, OLDX, OldY);
- if (!awakenscrollbars ()) {
- Invalidate ();
- }
- }
- }
It looks like if (mscrollx! = x | | mscrolly! = y) This judgment statement has no pass is the key. This also indirectly indicates that, when the activity is not initialized, some information obtained by the ScrollView object is inaccurate, which leads directly to the invalid Scrollto () method.
The ScrollView Scrollto method in Android doesn't work