/**
  * Reset the state of front view when the it's recycled by ListView
  *
  * @param frontView view to re-draw
  */
 protected void reloadSwipeStateInView(View frontView, int position) {
   if (!opened.get(position)) {
     setTranslationX(frontView, 0.0f);
   } else {
     if (openedRight.get(position)) {
       setTranslationX(frontView, swipeRecyclerView.getWidth());
     } else {
       setTranslationX(frontView, -swipeRecyclerView.getWidth());
     }
   }
 }
  /** @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;
  }