Android Scrollto Scrollby pull a tear

Source: Internet
Author: User

Not much nonsense to say, directly on the idea--

1: Imagine an interface, the parent layout is linearlayout, vertical arrangement, and then add two view inside, if I click the first view, directly call the second view of the Scrollto or Scrollby method, the second view will move it? Give the code and layout file---

Layout file:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"              android:layout_width= "Match_ Parent "              android:layout_height=" match_parent "              android:orientation=" vertical "              android:background=" # 00ff00 ">    <view            android:layout_width=" 200DP "            android:layout_height=" 50DP "            android:id=" @+ Id/button1 "            android:background=" #ff0000 "/>    <view            android:layout_width=" 200DP "            android: layout_height= "50DP"            android:id= "@+id/button2"            android:background= "#0000ff"/></linearlayout >

Three different colors are set, which makes it easy to see the effect, followed by the activity code

public class Activitytext extends Activity implements View.onclicklistener {Private view button1;private view button2; @Ov errideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_text); button1 = Findviewbyid (r.id.button1); button2 = Findviewbyid (R.id.button2); Button1.setonclicklistener (this);} @Overridepublic void OnClick (view view) {if (View.getid () = = R.id.button1) {//button2.scrollby (-30,-30); Button2.scrollto (30, 30);}}

Because very simple, here does not upload simple example, I directly said phenomenon: The second view of the mother is not moving ~, there is no effect at all--

I began to think, guess--is it necessary to move the parent container of the mobile view and the child in it will move? Okay, so I went through the second experiment--

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "              android:layout_width=" match_parent "              android:layout_height=" match_parent "              android:o rientation= "vertical"              android:background= "#00ff00"              android:id= "@+id/ll" >    <view            android: Layout_width= "200DP"            android:layout_height= "50DP"            android:id= "@+id/button1"            android:background= "# ff0000 "/>    <view            android:layout_width=" 200DP "            android:layout_height=" 50DP "            android:id = "@+id/button2"            android:background= "#0000ff"/></linearlayout>

Let's see, this time I added an ID to the two view's parent container, called LL, and then got the LinearLayout reference in the activity--

public class Activitytext extends Activity implements View.onclicklistener {Private View Button1;private view Button2;pri Vate linearlayout ll; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate) ; Setcontentview (r.layout.activity_text); ll = (LinearLayout) Findviewbyid (r.id.ll); button1 = Findviewbyid ( R.id.button1); button2 = Findviewbyid (R.id.button2); Button1.setonclicklistener (this);} @Overridepublic void OnClick (view view) {if (View.getid () = = R.id.button1) {Ll.scrollby (-30,-30);}}}

Notice the onclick code, inside I call LinearLayout Scrollby method, linearlayout inside of two view unexpectedly moved up, this is why? But so far it is OK to call ViewGroup's Scrollby or Scrollto method, and the child in the ViewGroup will move. If the direct call to the non-viewgroup view of the Scrollby or Scrollto method, is a little reaction is not--that I am wondering, is not viewgroup inside the source code in the Dispatchdraw () method to perform what special operation?

Check out ViewGroup's Dispatchdraw () method and discover some clues--

  int savecount = 0;2777        Final Boolean cliptopadding = (Flags & clip_to_padding_mask) = = clip_to_padding_mask;2778< C3/>if (cliptopadding) {2779            savecount = Canvas.save (); 2780            canvas.cliprect (mscrollx + mpaddingleft, MScrollY + mpaddingtop,2781                    mscrollx + mright-mleft-mpaddingright,2782                    mscrolly + mbottom-mtop-mpaddingbottom); 2783 2784        }

Pay attention to these lines of code, the total feeling is the reason here--now clear below, we directly call the view (non-viewgroup) Scrollto or Scrollby method is not effective, Description of the single view of the drawing there is no special operation of MSCROLLX and mscrolly these two variables--since it is not the problem of the view, look at the source code above, I can only find the problem is the viewgroup inside--again, We call the Scrollto or the Scrollby method to manipulate the two variables, MSCROLLX and mscrolly. I draw a conclusion from the above code: the original is the parent container to scroll the canvas and then draw their own children, so that the children will all move---and then someone will ask, if I do not want to move all the children, just want to move a second view? At this point you should think of a way, since it is the parent container to operate the child, then we just need to move the view of the outside to set a layer of view on it---below to verify the following our ideas--

Layout file:

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "              android:layout_width=" match_parent "              android:layout_height=" match_parent "              android:o rientation= "vertical"              android:background= "#00ff00"              android:id= "@+id/ll" >    <view            android: Layout_width= "200DP"            android:layout_height= "50DP"            android:id= "@+id/button1"            android:background= "# ff0000 "/>    <framelayout            android:layout_width=" match_parent "android:layout_height=" Match_            Parent "            android:background=" #000000 "            android:id=" @+id/fl ">        <view                android:layout_ Width= "200DP"                android:layout_height= "50DP"                android:id= "@+id/button2"                android:background= "# 0000ff "/>    </FrameLayout></LinearLayout>

This time I set a framelayout on the outside of the view I want to move, and then I get framelayout references in the activity--

public class Activitytext extends Activity implements View.onclicklistener {Private View Button1;private view Button2;pri Vate linearlayout ll;private framelayout fl; @Overrideprotected void onCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_text); ll = (LinearLayout) findViewById ( R.ID.LL); fl = (framelayout) Findviewbyid (R.ID.FL); button1 = Findviewbyid (r.id.button1); button2 = Findviewbyid ( R.id.button2); Button1.setonclicklistener (this);} @Overridepublic void OnClick (view view) {if (View.getid () = = R.id.button1) {Fl.scrollby (-30,-30);}}}

This time in the onclick event I call Framelayout's Scrollby method, thus achieving the purpose of letting the second view move--

The reader can experiment with the following, look at the effect--

Android Scrollto Scrollby pull a tear

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.