Android Learning Scroller (three)--control panning across the screen (scroller simple to use)

Source: Internet
Author: User

Mainactivity as follows:
Package Cc.cn;import Android.os.bundle;import Android.view.view;import android.view.view.onclicklistener;import Android.widget.button;import android.app.activity;/** * Demo Description: * Scroller Use example-let the control pan across the screen * reference: * HTTP://BLOG.CSDN.N et/c_weibin/article/details/7438323 * Thank you very much * * NOTE: * 1 Set the width of the Cc.cn.LinearLayoutSubClass control in the layout to "Fill_par    ENT "* Easy to observe the effect of sliding */public class Mainactivity extends Activity {private Button Mbutton; Private Linearlayoutsubclass mlinearlayoutsubclass; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.main); init ();} private void Init () {mlinearlayoutsubclass= (Linearlayoutsubclass) Findviewbyid (r.id.linearlayoutsubclass); mButton= (Button) Findviewbyid (R.id.button), Mbutton.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick ( View view) {Mlinearlayoutsubclass.beginscroll ();}});}}

Linearlayoutsubclass as follows:
Package Cc.cn;import Android.content.context;import Android.util.attributeset;import android.widget.LinearLayout; Import android.widget.scroller;/** * API NOTE: * * 1//First, two parameter start position; third, four scrolling offset; fifth parameter duration * Startscroll (int startX, int sta rty, int dx, int dy, int duration) * * 2//During the execution of the Startscroll () method The Computescrolloffset () * method will always return true during the duration time, but when the animation Returns False when execution is complete.  * Computescrolloffset () * * 3 when executing ontouch () or invalidate () or postinvalidate () will call the method * Computescroll () * */public class Linearlayoutsubclass extends LinearLayout {private Scroller mscroller;private boolean flag=true;public Linearlayoutsubclass (Context context) {super (context);} Public Linearlayoutsubclass (context context, AttributeSet Attrs) {Super (context, attrs);//You can also use this construction method to pass in a interpolator/ /mscroller=new Scroller (context, interpolator); mscroller=new Scroller (context);} @Overridepublic void Computescroll () {super.computescroll (); if (Mscroller.computescrolloffset ()) {ScrollTo ( Mscroller.getcurrx (), 0);//Make It call again Computescroll () until the end of the slide, i.e. not satisfying if condition postinvalidate ();}} public void Beginscroll () {if (flag) {mscroller.startscroll (0, 0, -2500, 0, 2500); flag = false;} else {Mscroller.startscrol L (0, 0, 0, 0,); flag = true;} Call invalidate (); Make it call Computescroll () invalidate ();}}

Main.xml as follows:
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" match_parent "> <Butt        On android:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:layout_centerhorizontal= "True" android:text= "click to Swipe"/> <cc.cn.linearlayoutsubclass A Ndroid:id= "@+id/linearlayoutsubclass" android:layout_width= "fill_parent" android:layout_height= "wrap_content            "Android:layout_centervertical=" true "> <textview android:layout_width=" wrap_content " android:layout_height= "Wrap_content" android:background= "#ff1100" android:text= "Test Scrolle R "/> </cc.cn.LinearLayoutSubClass></RelativeLayout>

Related Article

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.