Mainactivity as follows:
Package Cc.testscroller2;import Android.os.bundle;import android.app.activity;/** * Demo Description: * Implementation can pull back after the rebound layout. * Similar to the drop-down refresh. * * Reference: * 1 http://gundumw100.iteye.com/blog/1884373 * 2 http://blog.csdn.net/gemmem/article/details/7321910 * 3 http:/ /ipjmc.iteye.com/blog/1615828 * 4 http://blog.csdn.net/c_weibin/article/details/7438323 * 5 http://www.cnblogs.com/ wanqieddy/archive/2012/05/05/2484534.html * 6 http://blog.csdn.net/hudashi/article/details/7353075 * Thank you Very much */public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {Supe R.oncreate (savedinstancestate); Setcontentview (R.layout.main);}}
bounceablelinearlayout as follows:
Package Cc.testscroller2;import Android.content.Context; Import Android.util.AttributeSet; Import Android.view.GestureDetector; Import android.view.MotionEvent; Import Android.widget.LinearLayout; Import Android.widget.Scroller; /** * General idea: * Handle the Touch event of the view, that is, rewrite the Ontouchevent () method: * When the finger is lifted back to the origin, the rest is given to gesturedetector processing. * * The emphasis in Gesturedetector is to overwrite the Onscroll () method. In this method, the distance in the y direction is obtained, and the Mscroller.startscroll () method is set to prepare for sliding. * Refresh the interface invalidate () to execute Method Computescroll (). * The ScrollTo () method is called in the Computescroll () method to achieve true sliding. * * Note: * 1 ScrollTo () method parameters: * ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ()); * And after this also call Postinvalidate () to refresh the * * Method Description: * 1 mscroller.getfinalx (y) () * The final X (y) offset as an absolute di Stance from the origin. * Returns the scroll end position (Gets the value of the current x (Y) distance from the original position). Only for "Fling" scrolling is valid. * This means that the method is for scrolling end. * Coordinate direction: * Distance in X direction, positive to left, negative to right * Y direction, positive upward, negative number downward * * 2 Mscroller.getcurrx (Y) () * The new X offset as an Absol Ute distance from the origin. * The new Y offset as an absoluTe distance from the origin. * Returns the offset of the current scroll X (Y) direction * i.e. the method is for scrolling. * Coordinate direction: * Distance in x direction, positive to left, negative to right * y direction, positive upward, negative number down * * 3 invalidate () difference from postinvalidate () * Invalidate () in UI thread itself Postinvalidate () is used in a non-UI thread. * This is the current network of information on the general statement, still pending further study. * * 4 startscroll (int startX, int starty, int dx, int dy, int duration) * First, two parameter start position; third, four scrolling offset; fifth parameter Duration * * * * public class Bounceablelinearlayout extends LinearLayout {private Scroller mscroller; Private Gesturedetector Mgesturedetector; Public Bounceablelinearlayout (Context context) {This (context, NULL); } public Bounceablelinearlayout (context context, AttributeSet Attrs) {Super (context, attrs); Setclickable (TRUE); Setlongclickable (TRUE); Mscroller = new Scroller (context); Mgesturedetector = new Gesturedetector (context, new Gesturelistenerimpl ()); } @Override public void Computescroll () {if (Mscroller. Computescrolloffset ()) {System.out.println ("computescroll ()--->" + "mscroller.getc Urrx () = "+mscroller.getcurrx () +", "+" mscroller.getcurry () = "+mscroller.getcurry ()"); ScrollTo (Mscroller.getcurrx (), Mscroller.getcurry ()); Postinvalidate () must be executed to call Computescroll ()//In fact, call invalidate () here, or postinvalidate (); } super.computescroll (); } @Override public boolean ontouchevent (Motionevent event) {switch (event.getaction ()) { Case MOTIONEVENT.ACTION_UP://When the finger is lifted back to its original position preparescroll (0, 0); Break Default://The rest is handed to gesturedetector gesture processing return mgesturedetector.ontouchevent (event); } return Super.ontouchevent (event); } class Gesturelistenerimpl implements Gesturedetector.ongesturelistener {@Overridepublic Boolean ondown (Motioneven T e) {return true;} @Overridepublic void Onshowpress (Motionevent e) {} @Overridepublic Boolean onsingletapup (Motionevent e) {return false;} Control Pull Range://int disy= (int) ((distanceY-0.5)/2);//Can be called directly://smoothscrollby (0, (int) distancey); @Overridepublic Boolean Onscroll (motionevent E1, motionevent e2,float Distancex, float distancey) {int disy = (int) ((distanceY-0.5)/2); begins Croll (0, Disy); return false;} public void Onlongpress (Motionevent e) {} @Overridepublic boolean onfling (motionevent E1, motionevent E2, float Velocityx, Float Velocityy) {return false;}} Scroll to target position protected void Preparescroll (int fx, int fy) {int dx = FX-MSCROLLER.GETFINALX (); int dy = Fy-mscroller.getfinaly (); Beginscroll (dx, dy); }//Set the relative offset of the scrolling protected void beginscroll (int dx, int dy) {System.out.println ("Smoothscrollby ()--- ; dx= "+dx+", dy= "+dy); First, two parameter start position; third, four scrolling offset mscroller.startscroll (Mscroller.getfinalx (), mscroller.getfinaly (), DX, DY); System.out.prIntln ("Smoothscrollby ()--->" + "mscroller.getfinalx () =" +mscroller.getfinalx () + "," + "Mscroller.getfinaly () =" +mscroller.getfinaly ()); Invalidate () must be executed to invoke 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 " > <cc.testscroller2.bounceablelinearlayout android:layout_width= "Match_parent" android: layout_height= "Match_parent" > <textview android:layout_width= "wrap_content" android:layout_ height= "Wrap_content" android:text= "pull down and release" /> </cc.testscroller2.bounceablelinearlayout ></RelativeLayout>