Custom RecyclerView supports portrait orientation, sliding to delete Item, recyclerviewitem

Source: Internet
Author: User
Tags gety

Custom RecyclerView supports portrait orientation, sliding to delete Item, recyclerviewitem

Project source code has been put on: https://github.com/xufeifandj/SwipeRecyclerView


This project directly inherits from RecyclerView and supports a horizontal and vertical list.

For specific code, see the code.

Package com. ferris. holeswipeview; import android. content. context; import android. support. v7.widget. recyclerView; import android. util. attributeSet; import android. view. motionEvent; import android. view. velocityTracker; import android. view. view; import android. view. viewConfiguration; import android. view. windowManager; import android. widget. adapterView; import android. widget. scroller;/*** http://blog.csdn.net/x Ufeifandj www.github.com/xufeifandj ** @ ferris459821731@qq.com **/public class CusomSwipeView extends RecyclerView {private Orientation orientation = Orientation. HORIZONTAL;/*** currently sliding ListView position */private int slidePosition;/*** press the coordinates of X */private int downY; /*** Press Y's coordinate */private int downX;/***** screen width */private int screenWidth;/***** ListView's item */private View itemView; /* ** slide class */priv Ate Scroller scroller; private static final int SNAP_VELOCITY = 600;/***** speed tracking object */private VelocityTracker velocityTracker;/***** whether to respond to slide, the default value is "no response" */private boolean isSlide = false;/*** determines it is the minimum distance of user sliding */private int mTouchSlop; /*** callback interface after the item is removed */private RemoveListener mRemoveListener;/*** indicates the direction in which the item slides out of the screen, left or right, use an enumerated value to mark */private RemoveDirection removeDirection; // The enumerated value of the sliding Delete direction public enum RemoveDirectio N {RIGHT, LEFT;} public CusomSwipeView (Context context) {super (context); // TODO Auto-generated constructor stubinit (context);} public CusomSwipeView (Context context Context, attributeSet attrs) {super (context, attrs); // TODO Auto-generated constructor stubinit (context);} public CusomSwipeView (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); // TODO Auto-generated constructo R stubinit (context);} public void init (Context context) {if (orientation = Orientation. VERTICAL) {screenWidth = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE )). getdefadisplay display (). getWidth ();} else {screenWidth = (WindowManager) context. getSystemService (Context. WINDOW_SERVICE )). getdefadisplay display (). getHeight ();} scroller = new Scroller (context); mTouchSlop = ViewConfiguration. get (GetContext ()). getScaledTouchSlop ();}/*** sets the sliding deletion callback interface ** @ param removeListener */public void setRemoveListener (RemoveListener removeListener) {this. mRemoveListener = removeListener;}/*** distribution event, mainly used to determine the item clicked, and postDelayed to set the response to the left and right sliding event */@ Overridepublic boolean dispatchTouchEvent (MotionEvent event) {switch (event. getAction () {case MotionEvent. ACTION_DOWN: {addVelocityTracker (event); // If scrol Ler scrolling is not over yet. We directly return if (! Scroller. isFinished () {return super. dispatchTouchEvent (event);} downX = (int) event. getX (); downY = (int) event. getY (); itemView = findChildViewUnder (downX, downY); if (itemView = null) {return super. dispatchTouchEvent (event);} slidePosition = getChildPosition (itemView); // The position is invalid and will not be processed. if (slidePosition = AdapterView. INVALID_POSITION) {return super. dispatchTouchEvent (event);} break;} case MotionE Vent. ACTION_MOVE: {if (orientation = Orientation. VERTICAL) {// if the sliding distance between the left and right sides is greater than the maximum sliding distance, and there is no sliding up or down, it indicates that the sliding is deleted or the sliding is deleted if (Math. abs (getScrollVelocity ()> SNAP_VELOCITY | (Math. abs (event. getX ()-downX)> mTouchSlop & Math. abs (event. getY ()-downY) <mTouchSlop) {isSlide = true ;}} else {// if the distance between the upper and lower slides is greater than the maximum sliding distance, and the left and right slides are not performed, if (Math. abs (getScrollVelocity ()> SNAP_VELOCITY | (Math. abs (event. getY ()-DownY)> mTouchSlop & Math. abs (event. getX ()-downX) <mTouchSlop) {isSlide = true ;}} break;} case MotionEvent. ACTION_UP: recycleVelocityTracker (); break;} return super. dispatchTouchEvent (event);}/*** slide to the right. getScrollX () returns the distance from the left edge, that is, the distance from the left edge of the View to the start of sliding, therefore, sliding to the right is a negative value */private void scrollRight () {if (orientation = Orientation. VERTICAL) {// slide removeDirection = RemoveDirection to the right. RIGHT; final int delta = (ScreenWidth + itemView. getScrollX (); // call the startScroll method to set some rolling parameters. We call scrollTo in the computeScroll () method to scroll itemscroller. startScroll (itemView. getScrollX (), 0,-delta, 0, Math. abs (delta); postInvalidate (); // refresh itemView} else {// slide up removeDirection = RemoveDirection. RIGHT; final int delta = (screenWidth + itemView. getScrollY (); // call the startScroll method to set some rolling parameters. We call scrollTo in the computeScroll () method to scroll itemscroller. st ArtScroll (0, itemView. getScrollY (),-delta, 0, Math. abs (delta); postInvalidate (); // refresh itemView}/*** slides to the left. Based on the above information, sliding to the left is a positive value */private void scrollLeft () {if (orientation = Orientation. VERTICAL) {// move to the left: removeDirection = RemoveDirection. LEFT; final int delta = (screenWidth-itemView. getScrollX (); // call the startScroll method to set some rolling parameters. We call scrollTo in the computeScroll () method to scroll itemscroller. startScroll (itemView. getScro LlX (), 0, delta, 0, Math. abs (delta); postInvalidate (); // refresh itemView} else {removeDirection = RemoveDirection. LEFT; final int delta = (screenWidth-itemView. getScrollY (); // call the startScroll method to set some rolling parameters. We call scrollTo in the computeScroll () method to scroll itemscroller. startScroll (0, itemView. getScrollY (), delta, 0, Math. abs (delta); postInvalidate (); // refresh itemView}/*** use the finger to scroll the itemView distance to determine whether to scroll to the start position or to the left or right */private vo Id scrollByDistanceX () {// if the left-side scrolling distance is greater than 1/2 of the screen, delete if (orientation = Orientation. VERTICAL) {if (itemView. getScrollX ()> = screenWidth/2) {scrollLeft ();} else if (itemView. getScrollX () <=-screenWidth/2) {scrollRight ();} else {// roll back to the original location. To be lazy, you can call scrollTo to scroll itemView directly. scrollTo (0, 0) ;}} else {if (itemView. getScrollY ()> = screenWidth/2) {scrollLeft ();} else if (itemView. getScrollY () <=-screenW Idth/2) {scrollRight ();} else {// roll back to the original location. To steal the lazy information, call scrollTo to scroll itemView directly. scrollTo (0, 0) ;}}/ *** process the logic of dragging the ListView item */@ Overridepublic boolean onTouchEvent (MotionEvent ev) {if (isSlide & slidePosition! = AdapterView. INVALID_POSITION & itemView! = Null) {requestDisallowInterceptTouchEvent (true); addVelocityTracker (ev); final int action = ev. getAction (); int x = (int) ev. getX (); int y = (int) ev. getY (); switch (action) {case MotionEvent. ACTION_DOWN: break; case MotionEvent. ACTION_MOVE: MotionEvent cancelEvent = MotionEvent. obtain (ev); cancelEvent. setAction (MotionEvent. ACTION_CANCEL | (ev. getActionIndex () <MotionEvent. ACTION_POINTER_INDEX_SHIFT); on TouchEvent (cancelEvent); if (orientation = Orientation. VERTICAL) {int deltaX = downX-x; downX = x; // scroll itemView with your fingers. If deltaX is greater than 0, scroll to the left, and if deltaX is less than 0, roll itemView to the right. scrollBy (deltaX, 0);} else {int deltaY = downY-y; downY = y; itemView. scrollBy (0, deltaY);} return true; // when dragging, ListView does not scroll case MotionEvent. ACTION_UP: int velocityX = getScrollVelocity (); if (velocityX> SNAP_VELOCITY) {scrollRight ();} else if (velocityX <-SNAP_VELOCITY) {scrollLeft ();} else {scrollByDistanceX ();} recycleVelocityTracker (); // The isSlide = false; break is not returned when the finger leaves ;}} // otherwise, it will be handed over to ListView to handle the onTouchEvent return super. onTouchEvent (ev) ;}@ Overridepublic void computeScroll () {// scroller when startScroll is called. computescroloffset () returns true, if (scroller. computescroloffset () {// Let the ListView item scroll itemView based on the current rolling offset. scrollTo (scroller. getCurrX (), scroll Er. getCurrY (); postInvalidate (); // call the callback interface if (scroller. isFinished () {if (mRemoveListener = null) {throw new NullPointerException ("RemoveListener is null, we shocould called setRemoveListener ()");} itemView. scrollTo (0, 0); mRemoveListener. removeItem (removeDirection, slidePosition) ;}}/ *** Add a user's speed tracker ** @ param event */private void addVelocityTracker (MotionEvent event) {if (velocityTracker = Null) {velocityTracker = VelocityTracker. obtain ();} velocityTracker. addMovement (event);}/*** Remove User speed tracker */private void recycleVelocityTracker () {if (velocityTracker! = Null) {velocityTracker. recycle (); velocityTracker = null;}/*** get the sliding speed in the X direction. If the sliding speed is greater than 0, slide to the right, and vice versa to the left ** @ return */private int getScrollVelocity () {if (orientation = Orientation. VERTICAL) {velocityTracker. computeCurrentVelocity (1000); int velocity = (int) velocityTracker. getXVelocity (); return velocity;} else {velocityTracker. computeCurrentVelocity (1000); int velocity = (int) velocityTracker. getYVelocity (); return velocity ;}}/***** when the ListView item slides out of the screen, we need to remove this Item from the removeItem () callback method, then refresh the ListView ***/public interface RemoveListener {public void removeItem (RemoveDirection direction, int position);} public static enum Orientation {HORIZONTAL (0), VERTICAL (1 ); private int value; private Orientation (int I) {value = I;} public int value () {return value;} public static Orientation valueOf (int I) {switch (I) {case 0: return HORIZONTAL; case 1: return VERTICAL; default: throw new RuntimeException ("[0-> HORIZONTAL, 1-> VERTICAL]") ;}}}


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.