Android pull-down refresh and pull more, high imitation ios left sliding delete item, solves a lot of gesture Problems

Source: Internet
Author: User

Android pull-down refresh and pull more, high imitation ios left sliding delete item, solves a lot of gesture Problems

I. Preface

Old rules, let alone nothing else. This demo is made up of a collection of relevant knowledge. It can be said that it is a great headache for me! What programmers are talking about is no picture, no truth! Now everyone is happy!

II. The most important thing is the scroller class.

 

Package com. icq. slideview. view; import android. content. context; import android. util. attributeSet; import android. util. log; import android. util. typedValue; import android. view. motionEvent; import android. view. view; import android. widget. linearLayout; import android. widget. relativeLayout; import android. widget. scroller; import android. widget. textView; import com. icq. slideview. r; public class SlideView extends Li NearLayout {private static final String TAG = SlideView; private Context mContext; private LinearLayout mViewContent; private RelativeLayout mHolder; private Scroller roller; private OnSlideListener mOnSlideListener; private int mHolderWidth = 120; private int mLastX = 0; private int mLastY = 0; private static final int TAN = 2; public interface OnSlideListener {public static final int SLIDE _ STATUS_OFF = 0; public static final int SLIDE_STATUS_START_SCROLL = 1; public static final int SLIDE_STATUS_ON = 2; /*** @ param view current SlideView * @ param status SLIDE_STATUS_ON or SLIDE_STATUS_OFF */public void onSlide (View view, int status);} public SlideView (Context context) {super (context); initView ();} public SlideView (Context context, AttributeSet attrs) {super (context, attrs); in ItView ();} private void initView () {mContext = getContext (); mScroller = new Scroller (mContext); setOrientation (LinearLayout. HORIZONTAL); View. inflate (mContext, R. layout. slide_view_merge, this); mViewContent = (LinearLayout) findViewById (R. id. view_content); mHolderWidth = Math. round (TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, mHolderWidth, getResources (). getDisplayMetrics ();} pub Lic void setButtonText (CharSequence text) {(TextView) findViewById (R. id. delete )). setText (text);} public void setContentView (View view) {mViewContent. addView (view);} public void setOnSlideListener (OnSlideListener onSlideListener) {mOnSlideListener = onSlideListener;} public void shrink () {if (getScrollX ()! = 0) {this. smoothScrollTo (0, 0) ;}} public void onRequireTouchEvent (MotionEvent event) {int x = (int) event. getX (); int y = (int) event. getY (); int scrollX = getScrollX (); Log. d (TAG, x = + x + y = + y); switch (event. getAction () {case MotionEvent. ACTION_DOWN: {int deltaX = x-mLastX; System. out. println (Press Offset + deltaX); if (! MScroller. isFinished () {mScroller. abortAnimation ();} if (mOnSlideListener! = Null) {mOnSlideListener. onSlide (this, OnSlideListener. SLIDE_STATUS_START_SCROLL);} break;} case MotionEvent. ACTION_MOVE: {int deltaX = x-mLastX; int deltaY = y-mLastY; System. out. println (Offset + deltaX); if (Math. abs (deltaX) <Math. abs (deltaY) * TAN) {break;} int newScrollX = scrollX-deltaX; if (deltaX! = 0) {if (newScrollX <0) {newScrollX = 0;} else if (newScrollX> mHolderWidth) {newScrollX = mHolderWidth;} this. scrollTo (newScrollX, 0);} break;} case MotionEvent. ACTION_UP: {int newScrollX = 0; if (scrollX-mHolderWidth * 0.75> 0) {newScrollX = mHolderWidth;} this. smoothScrollTo (newScrollX, 0); if (mOnSlideListener! = Null) {mOnSlideListener. onSlide (this, newScrollX = 0? OnSlideListener. SLIDE_STATUS_OFF: OnSlideListener. SLIDE_STATUS_ON);} break;} default: break;} mLastX = x; mLastY = y;} private void smoothScrollTo (int destX, int destY) {// scroll slowly to the specified position int scrollX = getScrollX (); int delta = destX-scrollX; System. out. println (offset scrollX + scrollX); System. out. println (offset difference delta + delta); int a = Math. abs (delta) * 3; System. out. println (what does it mean + a); mScroller. startScroll (scrollX, 0, delta, 0, Math. abs (delta) * 3); invalidate () ;}@ Override public void computeScroll () {if (mScroller. computescroloffset () {System. out. println (current x position: + mScroller. getCurrX (); System. out. println (current Y position: + mScroller. getCurrY (); scrollTo (mScroller. getCurrX (), mScroller. getCurrY (); postInvalidate ();}}}

3. Download The Source Code directly: Recently, csdn is not so active in my opinion recently, and the real Daniel has rarely posted Weibo posts on csdn, they all went to github, stackoverflow, eoe, and other websites! Ah, I have to move my position later!

 

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.