/**
  * Close item
  *
  * @param position Position of list
  */
 protected void closeAnimate(int position) {
   closeAnimate(
       swipeListView
           .getChildAt(position - swipeListView.getFirstVisiblePosition())
           .findViewById(swipeFrontView),
       position);
 }
 /** Adds new items when adapter is modified */
 public void resetItems() {
   if (swipeListView.getAdapter() != null) {
     int count = swipeListView.getAdapter().getCount();
     for (int i = opened.size(); i <= count; i++) {
       opened.add(false);
       openedRight.add(false);
       checked.add(false);
     }
   }
 }
 /** Close all opened items */
 void closeOpenedItems() {
   if (opened != null) {
     int start = swipeListView.getFirstVisiblePosition();
     int end = swipeListView.getLastVisiblePosition();
     for (int i = start; i <= end; i++) {
       if (opened.get(i)) {
         closeAnimate(swipeListView.getChildAt(i - start).findViewById(swipeFrontView), i);
       }
     }
   }
 }
 /** Unselected choice state in item */
 protected int dismiss(int position) {
   int start = swipeListView.getFirstVisiblePosition();
   int end = swipeListView.getLastVisiblePosition();
   View view = swipeListView.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;
   }
 }
 /** Unselected choice state in item */
 protected void unselectedChoiceStates() {
   int start = swipeListView.getFirstVisiblePosition();
   int end = swipeListView.getLastVisiblePosition();
   for (int i = 0; i < checked.size(); i++) {
     if (checked.get(i) && i >= start && i <= end) {
       reloadChoiceStateInView(
           swipeListView.getChildAt(i - start).findViewById(swipeFrontView), i);
     }
     checked.set(i, false);
   }
   swipeListView.onChoiceEnded();
   returnOldActions();
 }
 /**
  * Constructor
  *
  * @param swipeListView SwipeListView
  * @param swipeFrontView front view Identifier
  * @param swipeBackView back view Identifier
  */
 public SwipeListViewTouchListener(
     SwipeListView swipeListView, int swipeFrontView, int swipeBackView) {
   this.swipeFrontView = swipeFrontView;
   this.swipeBackView = swipeBackView;
   ViewConfiguration vc = ViewConfiguration.get(swipeListView.getContext());
   slop = vc.getScaledTouchSlop();
   minFlingVelocity = vc.getScaledMinimumFlingVelocity();
   maxFlingVelocity = vc.getScaledMaximumFlingVelocity();
   configShortAnimationTime =
       swipeListView
           .getContext()
           .getResources()
           .getInteger(android.R.integer.config_shortAnimTime);
   animationTime = configShortAnimationTime;
   this.swipeListView = swipeListView;
 }
  private void removePendingDismisses(int originalHeight) {
    // No active animations, process all pending dismisses.
    // Sort by descending position
    Collections.sort(pendingDismisses);

    int[] dismissPositions = new int[pendingDismisses.size()];
    for (int i = pendingDismisses.size() - 1; i >= 0; i--) {
      dismissPositions[i] = pendingDismisses.get(i).position;
    }
    swipeListView.onDismiss(dismissPositions);

    ViewGroup.LayoutParams lp;
    for (PendingDismissData pendingDismiss : pendingDismisses) {
      // Reset view presentation
      if (pendingDismiss.view != null) {
        setAlpha(pendingDismiss.view, 1f);
        setTranslationX(pendingDismiss.view, 0);
        lp = pendingDismiss.view.getLayoutParams();
        lp.height = originalHeight;
        pendingDismiss.view.setLayoutParams(lp);
      }
    }

    resetPendingDismisses();
  }
 /**
  * Swap choice state in item
  *
  * @param position position of list
  */
 private void swapChoiceState(int position) {
   int lastCount = getCountSelected();
   boolean lastChecked = checked.get(position);
   checked.set(position, !lastChecked);
   int count = lastChecked ? lastCount - 1 : lastCount + 1;
   if (lastCount == 0 && count == 1) {
     swipeListView.onChoiceStarted();
     closeOpenedItems();
     setActionsTo(SwipeListView.SWIPE_ACTION_CHOICE);
   }
   if (lastCount == 1 && count == 0) {
     swipeListView.onChoiceEnded();
     returnOldActions();
   }
   if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
     swipeListView.setItemChecked(position, !lastChecked);
   }
   swipeListView.onChoiceChanged(position, !lastChecked);
   reloadChoiceStateInView(frontView, position);
 }
 /**
  * Moves the view
  *
  * @param deltaX delta
  */
 public void move(float deltaX) {
   swipeListView.onMove(downPosition, deltaX);
   float posX = ViewHelper.getX(frontView);
   if (opened.get(downPosition)) {
     posX += openedRight.get(downPosition) ? -viewWidth + rightOffset : viewWidth - leftOffset;
   }
   if (posX > 0 && !swipingRight) {
     Log.d("SwipeListView", "change to right");
     swipingRight = !swipingRight;
     swipeCurrentAction = swipeActionRight;
     if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
       backView.setVisibility(View.GONE);
     } else {
       backView.setVisibility(View.VISIBLE);
     }
   }
   if (posX < 0 && swipingRight) {
     Log.d("SwipeListView", "change to left");
     swipingRight = !swipingRight;
     swipeCurrentAction = swipeActionLeft;
     if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
       backView.setVisibility(View.GONE);
     } else {
       backView.setVisibility(View.VISIBLE);
     }
   }
   if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_DISMISS) {
     setTranslationX(parentView, deltaX);
     setAlpha(parentView, Math.max(0f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / viewWidth)));
   } else if (swipeCurrentAction == SwipeListView.SWIPE_ACTION_CHOICE) {
     if ((swipingRight && deltaX > 0 && posX < DISPLACE_CHOICE)
         || (!swipingRight && deltaX < 0 && posX > -DISPLACE_CHOICE)
         || (swipingRight && deltaX < DISPLACE_CHOICE)
         || (!swipingRight && deltaX > -DISPLACE_CHOICE)) {
       setTranslationX(frontView, deltaX);
     }
   } else {
     setTranslationX(frontView, deltaX);
   }
 }
  /** @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 = swipeListView.getWidth();
    }

    switch (MotionEventCompat.getActionMasked(motionEvent)) {
      case MotionEvent.ACTION_DOWN:
        {
          if (paused && downPosition != ListView.INVALID_POSITION) {
            return false;
          }
          swipeCurrentAction = SwipeListView.SWIPE_ACTION_NONE;

          int childCount = swipeListView.getChildCount();
          int[] listViewCoords = new int[2];
          swipeListView.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 = swipeListView.getChildAt(i);
            child.getHitRect(rect);

            int childPosition = swipeListView.getPositionForView(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;

            if (allowSwipe && rect.contains(x, y)) {
              setParentView(child);
              setFrontView(child.findViewById(swipeFrontView));

              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 == SwipeListView.SWIPE_MODE_LEFT && velocityTracker.getXVelocity() > 0) {
              velocityX = 0;
            }
            if (swipeMode == SwipeListView.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;
            Log.d("SwipeListView", "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 == SwipeListView.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 = swipeListView.changeSwipeMode(downPosition);
          if (changeSwipeMode >= 0) {
            swipeMode = changeSwipeMode;
          }

          if (swipeMode == SwipeListView.SWIPE_MODE_NONE) {
            deltaMode = 0;
          } else if (swipeMode != SwipeListView.SWIPE_MODE_BOTH) {
            if (opened.get(downPosition)) {
              if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX < 0) {
                deltaMode = 0;
              } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX > 0) {
                deltaMode = 0;
              }
            } else {
              if (swipeMode == SwipeListView.SWIPE_MODE_LEFT && deltaX > 0) {
                deltaMode = 0;
              } else if (swipeMode == SwipeListView.SWIPE_MODE_RIGHT && deltaX < 0) {
                deltaMode = 0;
              }
            }
          }
          if (deltaMode > slop
              && swipeCurrentAction == SwipeListView.SWIPE_ACTION_NONE
              && velocityY < velocityX) {
            swiping = true;
            swipingRight = (deltaX > 0);
            Log.d("SwipeListView", "deltaX: " + deltaX + " - swipingRight: " + swipingRight);
            if (opened.get(downPosition)) {
              swipeListView.onStartClose(downPosition, swipingRight);
              swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
            } else {
              if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_DISMISS) {
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
              } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_DISMISS) {
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_DISMISS;
              } else if (swipingRight && swipeActionRight == SwipeListView.SWIPE_ACTION_CHOICE) {
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
              } else if (!swipingRight && swipeActionLeft == SwipeListView.SWIPE_ACTION_CHOICE) {
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_CHOICE;
              } else {
                swipeCurrentAction = SwipeListView.SWIPE_ACTION_REVEAL;
              }
              swipeListView.onStartOpen(downPosition, swipeCurrentAction, swipingRight);
            }
            swipeListView.requestDisallowInterceptTouchEvent(true);
            MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
            cancelEvent.setAction(
                MotionEvent.ACTION_CANCEL
                    | (MotionEventCompat.getActionIndex(motionEvent)
                        << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
            swipeListView.onTouchEvent(cancelEvent);
            if (swipeCurrentAction == SwipeListView.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;
        }
    }
    return false;
  }