Scroller (4) for Android learning-layout of rebound after pulling

Source: Internet
Author: User

Scroller (4) for Android learning-layout of rebound after pulling

MainActivity is as follows:

Package cc. testscroller2; import android. OS. bundle; import android. app. activity;/*** Demo Description: * The layout that can be pulled and then rebounded. * similar to the pull-down refresh. ** references: * 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) {super. onCreate (savedInstanceState); setContentView (R. layout. main );}}


BounceableLinearLayout is 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;/*** overall idea: * process the Touch event of the View, that is, rewrite the onTouchEvent () method: * When the finger is lifted, return it to the origin, and submit the rest to GestureDetector for processing. ** in GestureDetector, the focus is to override the onScroll () method. in this method, the * Y sliding distance is obtained, and then the mscroroller is set. startScroll () method, ready to slide. * refresh the invalidate () interface to execute the computeScroll () method (). * call the scrollTo () method in the computeScroll () method to achieve real sliding. ** Note: * 1 parameter of the scrollTo () method: * scrollTo (mScroller. getCurrX (), mScroller. getCurrY (); * and then call postInvalidate () to refresh. *** method Description: * 1 mScroller. getFinalX (Y) () * The final X (Y) offset as an absolute distance from the origin. * return the scroll end position (get the value of the current X (Y) Distance from the original position ). valid only for "fling" scrolling. * That is to say, this method is for the rolling end. ** direction of Parallelism: ** the distance in the X direction. The distance between positive numbers and negative numbers to the right * Y direction. The distance between positive numbers and negative numbers is ** 2 mScroller. getCurrX (Y) () * The new X offset as an absolute distance from the origin. * The new Y offset as an absolute distance from the origin. * returns the offset of the current scrolling X (Y) Direction * that is, this method is applicable to scrolling. ** orientation: ** the distance from X to left. The distance from a positive number to left and from a negative number to the right X to Y. The distance from a positive number to an upward number, and from a negative number to a downward ** 3 invalidate () and postInvalidate () difference * invalidate () is used in the UI thread itself; postInvalidate () is used in non-UI threads. * This is a common saying about network data. Further research is needed. ** 4 startScroll (int startX, int startY, int dx, int dy, int duration) * start position of the first and second parameters. Third, offset of the four scrolls; duration of the fifth parameter ***/public class BounceableLinearLayout extends LinearLayout {private Scroller mScroller; private GestureDetector mGestureDetector; public listener (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. computescroloffset () {System. out. println ("computeScroll () --->" + "mScroller. getCurrX () = "+ mScroller. getCurrX () + "," + "mScroller. getCurrY () = "+ mScroller. getCurrY (); scrollTo (mScroller. getCurrX (), mScroller. getCurrY (); // You must execute postInvalidate () to call computeScroll () // actually, call invalidate () here; or postInvalidate ();} super. computeScroll () ;}@ Override public boolean onTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_UP: // return to the initial position when the finger is lifted (0, 0); break; default: // in other cases, hand it to GestureDetector for gesture processing return mGestureDetector. onTouchEvent (event);} return super. onTouchEvent (event);} class GestureListenerImpl implements GestureDetector. onGestureListener {@ Overridepublic boolean onDown (MotionEvent e) {return true;} @ Overridepublic void onShowPress (MotionEvent e) {}@ Overridepublic boolean onSingleTapUp (MotionEvent e) {return false ;} // control the pull margin: // int disY = (int) (distanceY-0.5)/2); // You can also directly call: // smoothScrollBy (0, (int) distanceY); @ Overridepublic boolean onScroll (MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {int disY = (int) (distanceY-0.5)/2 ); beginScroll (0, disY); return false;} public void onLongPress (MotionEvent e) {}@ Overridepublic boolean onFling (MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {return false ;}// scroll to the target location protected void prepareScroll (int fx, int fy) {int dx = fx-mScroller. getFinalX (); int dy = fy-mScroller. getFinalY (); beginScroll (dx, dy);} // you can specify the relative offset protected void beginScroll (int dx, int dy) {System. out. println ("smoothScrollBy () ---> dx =" + dx + ", dy =" + dy); // first, the starting position of the two parameters; third, the offset of the four rollbacks. startScroll (mScroller. getFinalX (), mScroller. getFinalY (), dx, dy); System. out. println ("smoothScrollBy () --->" + "mScroller. getFinalX () = "+ mScroller. getFinalX () + "," + "mScroller. getFinalY () = "+ mScroller. getFinalY (); // You must execute invalidate () to call computeScroll () invalidate ();}}


Main. xml is as follows:

     
          
               
  
 


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.