Skip to content

Commit

Permalink
Merge pull request nhaarman#233 from BraisGabin/no-dismissable-animation
Browse files Browse the repository at this point in the history
Feature: animate the no dismissable items
  • Loading branch information
nhaarman committed Aug 15, 2014
2 parents 31f7dde + 14887f9 commit bf0afc9
Showing 1 changed file with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ public abstract class SwipeTouchListener implements View.OnTouchListener, TouchE
*/
private boolean mSwiping;

/**
* Indicates whether the user can dismiss the current item.
*/
private boolean mCanDismissCurrent;

/**
* The {@code VelocityTracker} used in the swipe movement.
*/
Expand Down Expand Up @@ -329,9 +334,7 @@ private boolean handleDownEvent(@Nullable final View view, @NonNull final Motion
}

int downPosition = AdapterViewUtil.getPositionForView(mListViewWrapper, downView);
if (!isDismissable(downPosition)) {
return false;
}
mCanDismissCurrent = isDismissable(downPosition);

/* Check if we are processing the item at this position */
if (mCurrentPosition == downPosition || downPosition >= mVirtualListCount) {
Expand Down Expand Up @@ -447,8 +450,12 @@ private boolean handleMoveEvent(@Nullable final View view, @NonNull final Motion
}

if (mSwiping) {
ViewHelper.setTranslationX(mSwipingView, deltaX);
ViewHelper.setAlpha(mSwipingView, Math.max(mMinimumAlpha, Math.min(1, 1 - 2 * Math.abs(deltaX) / mViewWidth)));
if (mCanDismissCurrent) {
ViewHelper.setTranslationX(mSwipingView, deltaX);
ViewHelper.setAlpha(mSwipingView, Math.max(mMinimumAlpha, Math.min(1, 1 - 2 * Math.abs(deltaX) / mViewWidth)));
} else {
ViewHelper.setTranslationX(mSwipingView, deltaX * 0.1f);
}
return true;
}
return false;
Expand All @@ -474,23 +481,25 @@ private boolean handleUpEvent(@NonNull final MotionEvent motionEvent) {
}

if (mSwiping) {
float deltaX = motionEvent.getRawX() - mDownX;
boolean shouldDismiss = false;
boolean dismissToRight = false;

mVelocityTracker.addMovement(motionEvent);
mVelocityTracker.computeCurrentVelocity(1000);
if (mCanDismissCurrent) {
float deltaX = motionEvent.getRawX() - mDownX;

float velocityX = Math.abs(mVelocityTracker.getXVelocity());
float velocityY = Math.abs(mVelocityTracker.getYVelocity());
mVelocityTracker.addMovement(motionEvent);
mVelocityTracker.computeCurrentVelocity(1000);

boolean shouldDismiss = false;
boolean dismissToRight = false;
float velocityX = Math.abs(mVelocityTracker.getXVelocity());
float velocityY = Math.abs(mVelocityTracker.getYVelocity());

if (Math.abs(deltaX) > mViewWidth / 2) {
shouldDismiss = true;
dismissToRight = deltaX > 0;
} else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX) {
shouldDismiss = true;
dismissToRight = mVelocityTracker.getXVelocity() > 0;
if (Math.abs(deltaX) > mViewWidth / 2) {
shouldDismiss = true;
dismissToRight = deltaX > 0;
} else if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity && velocityY < velocityX) {
shouldDismiss = true;
dismissToRight = mVelocityTracker.getXVelocity() > 0;
}
}


Expand Down Expand Up @@ -575,6 +584,7 @@ private void reset() {
mSwipingView = null;
mCurrentPosition = AdapterView.INVALID_POSITION;
mSwiping = false;
mCanDismissCurrent = false;
}

/**
Expand Down

0 comments on commit bf0afc9

Please sign in to comment.