/** * Open item * * @param position Position of list */ protected void openAnimate(int position) { final View child = swipeRecyclerView .getChildAt(position - mLayoutManager.findFirstVisibleItemPosition()) .findViewById(swipeFrontView); if (child != null) { openAnimate(child, position); } }
/** Close all opened items */ void closeOpenedItems() { if (opened != null) { int start = mLayoutManager.findFirstVisibleItemPosition(); int end = mLayoutManager.findLastVisibleItemPosition(); for (int i = start; i <= end; i++) { if (opened.get(i)) { closeAnimate(swipeRecyclerView.getChildAt(i - start).findViewById(swipeFrontView), i); } } } }
private void closeOtherOpenedItems() { if (opened != null && downPosition != SwipeRecyclerView.NO_POSITION) { int start = mLayoutManager.findFirstVisibleItemPosition(); int end = mLayoutManager.findLastVisibleItemPosition(); for (int i = start; i <= end; i++) { if (opened.get(i) && i != downPosition) { closeAnimate(swipeRecyclerView.getChildAt(i - start).findViewById(swipeFrontView), i); } } } }
/** Unselected choice state in item */ protected void unselectedChoiceStates() { int start = mLayoutManager.findFirstVisibleItemPosition(); int end = mLayoutManager.findLastVisibleItemPosition(); for (int i = 0; i < checked.size(); i++) { if (checked.get(i) && i >= start && i <= end) { reloadChoiceStateInView( swipeRecyclerView.getChildAt(i - start).findViewById(swipeFrontView), i); } checked.set(i, false); } swipeRecyclerView.onChoiceEnded(); returnOldActions(); }
/** * Close item * * @param position Position of list */ protected void closeAnimate(int position) { if (swipeRecyclerView != null) { int firstVisibleChildPosition = mLayoutManager.findFirstVisibleItemPosition(); final View childContainer = swipeRecyclerView.getChildAt(position - firstVisibleChildPosition); if (childContainer != null) { final View child = childContainer.findViewById(swipeFrontView); if (child != null) { closeAnimate(child, position); } } } }
/** * Dismiss an item. * * @param position is the position of the item to delete. * @return 0 if the item is not visible. Otherwise return the height of the cell to dismiss. */ protected int dismiss(int position) { opened.remove(position); checked.remove(position); int start = mLayoutManager.findFirstVisibleItemPosition(); int end = mLayoutManager.findLastVisibleItemPosition(); View view = swipeRecyclerView.getChildAt(position - start); ++dismissAnimationRefCount; if (position >= start && position <= end) { performDismiss(view, position, false); return view.getHeight(); } else { pendingDismisses.add(new PendingDismissData(position, null)); return 0; } }
/** @see View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent) */ @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (!isSwipeEnabled()) { return false; } if (viewWidth < 2) { viewWidth = swipeRecyclerView.getWidth(); } switch (MotionEventCompat.getActionMasked(motionEvent)) { case MotionEvent.ACTION_DOWN: { if (paused && downPosition != ListView.INVALID_POSITION) { return false; } swipeCurrentAction = SwipeRecyclerView.SWIPE_ACTION_NONE; int childCount = swipeRecyclerView.getChildCount(); int[] listViewCoords = new int[2]; swipeRecyclerView.getLocationOnScreen(listViewCoords); int x = (int) motionEvent.getRawX() - listViewCoords[0]; int y = (int) motionEvent.getRawY() - listViewCoords[1]; View child; for (int i = 0; i < childCount; i++) { child = swipeRecyclerView.getChildAt(i); child.getHitRect(rect); int childPosition = swipeRecyclerView.getChildPosition(child); // dont allow swiping if this is on the header or footer or IGNORE_ITEM_VIEW_TYPE or // enabled is false on the adapter // boolean allowSwipe = swipeListView.getAdapter().isEnabled(childPosition) && // swipeListView.getAdapter().getItemViewType(childPosition) >= 0; boolean allowSwipe = true; if (allowSwipe && rect.contains(x, y)) { setParentView(child); setFrontView(child.findViewById(swipeFrontView), childPosition); downX = motionEvent.getRawX(); downPosition = childPosition; frontView.setClickable(!opened.get(downPosition)); frontView.setLongClickable(!opened.get(downPosition)); velocityTracker = VelocityTracker.obtain(); velocityTracker.addMovement(motionEvent); if (swipeBackView > 0) { setBackView(child.findViewById(swipeBackView)); } break; } } view.onTouchEvent(motionEvent); return true; } case MotionEvent.ACTION_UP: { if (velocityTracker == null || !swiping || downPosition == ListView.INVALID_POSITION) { break; } float deltaX = motionEvent.getRawX() - downX; velocityTracker.addMovement(motionEvent); velocityTracker.computeCurrentVelocity(1000); float velocityX = Math.abs(velocityTracker.getXVelocity()); if (!opened.get(downPosition)) { if (swipeMode == SwipeRecyclerView.SWIPE_MODE_LEFT && velocityTracker.getXVelocity() > 0) { velocityX = 0; } if (swipeMode == SwipeRecyclerView.SWIPE_MODE_RIGHT && velocityTracker.getXVelocity() < 0) { velocityX = 0; } } float velocityY = Math.abs(velocityTracker.getYVelocity()); boolean swap = false; boolean swapRight = false; if (minFlingVelocity <= velocityX && velocityX <= maxFlingVelocity && velocityY * 2 < velocityX) { swapRight = velocityTracker.getXVelocity() > 0; if (SwipeRecyclerView.DEBUG) { Log.d( SwipeRecyclerView.TAG, "swapRight: " + swapRight + " - swipingRight: " + swipingRight); } if (swapRight != swipingRight && swipeActionLeft != swipeActionRight) { swap = false; } else if (opened.get(downPosition) && openedRight.get(downPosition) && swapRight) { swap = false; } else if (opened.get(downPosition) && !openedRight.get(downPosition) && !swapRight) { swap = false; } else { swap = true; } } else if (Math.abs(deltaX) > viewWidth / 2) { swap = true; swapRight = deltaX > 0; } generateAnimate(frontView, swap, swapRight, downPosition); if (swipeCurrentAction == SwipeRecyclerView.SWIPE_ACTION_CHOICE) { swapChoiceState(downPosition); } velocityTracker.recycle(); velocityTracker = null; downX = 0; // change clickable front view // if (swap) { // frontView.setClickable(opened.get(downPosition)); // frontView.setLongClickable(opened.get(downPosition)); // } swiping = false; break; } case MotionEvent.ACTION_MOVE: { if (velocityTracker == null || paused || downPosition == ListView.INVALID_POSITION) { break; } velocityTracker.addMovement(motionEvent); velocityTracker.computeCurrentVelocity(1000); float velocityX = Math.abs(velocityTracker.getXVelocity()); float velocityY = Math.abs(velocityTracker.getYVelocity()); float deltaX = motionEvent.getRawX() - downX; float deltaMode = Math.abs(deltaX); int swipeMode = this.swipeMode; int changeSwipeMode = swipeRecyclerView.changeSwipeMode(downPosition); if (changeSwipeMode >= 0) { swipeMode = changeSwipeMode; } if (swipeMode == SwipeRecyclerView.SWIPE_MODE_NONE) { deltaMode = 0; } else if (swipeMode != SwipeRecyclerView.SWIPE_MODE_BOTH) { if (opened.get(downPosition)) { if (swipeMode == SwipeRecyclerView.SWIPE_MODE_LEFT && deltaX < 0) { deltaMode = 0; } else if (swipeMode == SwipeRecyclerView.SWIPE_MODE_RIGHT && deltaX > 0) { deltaMode = 0; } } else { if (swipeMode == SwipeRecyclerView.SWIPE_MODE_LEFT && deltaX > 0) { deltaMode = 0; } else if (swipeMode == SwipeRecyclerView.SWIPE_MODE_RIGHT && deltaX < 0) { deltaMode = 0; } } } if (deltaMode > slop && swipeCurrentAction == SwipeRecyclerView.SWIPE_ACTION_NONE && velocityY < velocityX) { swiping = true; swipingRight = (deltaX > 0); if (SwipeRecyclerView.DEBUG) { Log.d( SwipeRecyclerView.TAG, "deltaX: " + deltaX + " - swipingRight: " + swipingRight); } if (opened.get(downPosition)) { swipeRecyclerView.onStartClose(downPosition, swipingRight); swipeCurrentAction = SwipeRecyclerView.SWIPE_ACTION_REVEAL; } else { if (swipingRight && swipeActionRight == SwipeRecyclerView.SWIPE_ACTION_DISMISS) { swipeCurrentAction = SwipeRecyclerView.SWIPE_ACTION_DISMISS; } else if (!swipingRight && swipeActionLeft == SwipeRecyclerView.SWIPE_ACTION_DISMISS) { swipeCurrentAction = SwipeRecyclerView.SWIPE_ACTION_DISMISS; } else if (swipingRight && swipeActionRight == SwipeRecyclerView.SWIPE_ACTION_CHOICE) { swipeCurrentAction = SwipeRecyclerView.SWIPE_ACTION_CHOICE; } else if (!swipingRight && swipeActionLeft == SwipeRecyclerView.SWIPE_ACTION_CHOICE) { swipeCurrentAction = SwipeRecyclerView.SWIPE_ACTION_CHOICE; } else { swipeCurrentAction = SwipeRecyclerView.SWIPE_ACTION_REVEAL; } swipeRecyclerView.onStartOpen(downPosition, swipeCurrentAction, swipingRight); } swipeRecyclerView.requestDisallowInterceptTouchEvent(true); MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); cancelEvent.setAction( MotionEvent.ACTION_CANCEL | (MotionEventCompat.getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT)); swipeRecyclerView.onTouchEvent(cancelEvent); if (swipeCurrentAction == SwipeRecyclerView.SWIPE_ACTION_CHOICE) { backView.setVisibility(View.GONE); } } if (swiping && downPosition != ListView.INVALID_POSITION) { if (opened.get(downPosition)) { deltaX += openedRight.get(downPosition) ? viewWidth - rightOffset : -viewWidth + leftOffset; } move(deltaX); return true; } break; } } if (onlyOneOpenedWhenSwipe) { closeOtherOpenedItems(); view.onTouchEvent(motionEvent); return true; } return false; }