The ScrollView Scrollto method in Android doesn't work

Source: Internet
Author: User

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
    1. Mscrollview.post (new Runnable () {
    2. @Override
    3. public Void Run () {
    4. Mscrollview.scrollto (0, 1000);
    5. }
    6. });

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
  1. /**
  2. * {@inheritDoc}
  3. *
  4. * <p>this version also clamps the scrolling to the bounds of our child.
  5. */
  6. @Override
  7. Public void ScrollTo (int x, int y) {
  8. //We rely on the fact the View.scrollby calls ScrollTo.
  9. if (Getchildcount () > 0) {
  10. View child = Getchildat (0);
  11. x = Clamp (x, GetWidth ()-Mpaddingright-mpaddingleft, Child.getwidth ());
  12. y = Clamp (y, getheight ()-Mpaddingbottom-mpaddingtop, Child.getheight ());
  13. if (x! = MSCROLLX | | Y! = mscrolly) {
  14. Super.scrollto (x, y);
  15. }
  16. }
  17. }

Its parent-Class View

[Java]View Plaincopy
  1. Public void ScrollTo (int x, int y) {
  2. if (mscrollx! = x | | mscrolly! = y) {
  3. int oldx = MSCROLLX;
  4. int oldY = mscrolly;
  5. MSCROLLX = x;
  6. mscrolly = y;
  7. Onscrollchanged (MSCROLLX, mscrolly, OLDX, OldY);
  8. if (!awakenscrollbars ()) {
  9. Invalidate ();
  10. }
  11. }
  12. }

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

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.