diff --git a/README.md b/README.md index e6525c1..d987253 100644 --- a/README.md +++ b/README.md @@ -230,9 +230,15 @@ Additional Options ### setFadeOut(boolean fadeOut) Setting this to true will cause the ListView item to slowly fade out as it is being swiped. -### setFixedBackground(boolean setFixedBackground) +### setFixedBackgrounds(boolean setFixedBackgrounds) Setting this to true will make the backgrounds static behind the ListView item instead of sliding in from the side. +### setDimBackgrounds(boolean setFixedBackgrounds) +Setting this to true will make the backgrounds appear dimmed before the normal swipe threshold is reached. + +### setNormalSwipeFraction(float normalSwipeFraction) +Allows you to set the fraction of the view width that must be swiped before it is counted as a normal swipe. The float must be between 0 and 1. 0 makes every swipe register, 1 effectively disables swipe. + ### setFarSwipeFraction(float farSwipeFraction) Allows you to set the fraction of the view width that must be swiped before it is counted as a far swipe. The float must be between 0 and 1. 0 makes every swipe a far swipe, 1 effectively disables a far swipe. diff --git a/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeActionAdapter.java b/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeActionAdapter.java index 92b8828..e2a66a6 100644 --- a/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeActionAdapter.java +++ b/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeActionAdapter.java @@ -36,7 +36,9 @@ public class SwipeActionAdapter extends DecoratorAdapter implements protected SwipeActionListener mSwipeActionListener; private boolean mFadeOut = false; private boolean mFixedBackgrounds = false; + private boolean mDimBackgrounds = false; private float mFarSwipeFraction = 0.5f; + private float mNormalSwipeFraction = 0.25f; protected SparseIntArray mBackgroundResIds = new SparseIntArray(); @@ -119,12 +121,25 @@ public SwipeActionAdapter setFadeOut(boolean mFadeOut){ * Set whether the backgrounds should be fixed or swipe in from the side * The default value for this property is false: backgrounds will swipe in * - * @param mFixedBackgrounds true for fixed backgrounds, false for swipe in + * @param fixedBackgrounds true for fixed backgrounds, false for swipe in */ @SuppressWarnings("unused") - public SwipeActionAdapter setFixedBackgrounds(boolean mFixedBackgrounds){ - this.mFixedBackgrounds = mFixedBackgrounds; - if(mListView != null) mTouchListener.setFixedBackgrounds(mFixedBackgrounds); + public SwipeActionAdapter setFixedBackgrounds(boolean fixedBackgrounds){ + this.mFixedBackgrounds = fixedBackgrounds; + if(mListView != null) mTouchListener.setFixedBackgrounds(fixedBackgrounds); + return this; + } + + /** + * Set whether the backgrounds should be dimmed when in no-trigger zone + * The default value for this property is false: backgrounds will not dim + * + * @param dimBackgrounds true for dimmed backgrounds, false for no opacity change + */ + @SuppressWarnings("unused") + public SwipeActionAdapter setDimBackgrounds(boolean dimBackgrounds){ + this.mDimBackgrounds = dimBackgrounds; + if(mListView != null) mTouchListener.setDimBackgrounds(dimBackgrounds); return this; } @@ -138,25 +153,43 @@ public SwipeActionAdapter setFarSwipeFraction(float farSwipeFraction) { if(farSwipeFraction < 0 || farSwipeFraction > 1) { throw new IllegalArgumentException("Must be a float between 0 and 1"); } - mFarSwipeFraction = farSwipeFraction; - if(mListView != null) mTouchListener.setFarSwipeFraction(mFarSwipeFraction); + this.mFarSwipeFraction = farSwipeFraction; + if(mListView != null) mTouchListener.setFarSwipeFraction(farSwipeFraction); + return this; + } + + /** + * Set the fraction of the View Width that needs to be swiped before it is counted as a normal swipe + * + * @param normalSwipeFraction float between 0 and 1 + */ + @SuppressWarnings("unused") + public SwipeActionAdapter setNormalSwipeFraction(float normalSwipeFraction) { + if(normalSwipeFraction < 0 || normalSwipeFraction > 1) { + throw new IllegalArgumentException("Must be a float between 0 and 1"); + } + this.mNormalSwipeFraction = normalSwipeFraction; + if(mListView != null) mTouchListener.setNormalSwipeFraction(normalSwipeFraction); return this; } /** * We need the ListView to be able to modify it's OnTouchListener * - * @param mListView the ListView to which the adapter will be attached + * @param listView the ListView to which the adapter will be attached * @return A reference to the current instance so that commands can be chained */ - public SwipeActionAdapter setListView(ListView mListView){ - this.mListView = mListView; - mTouchListener = new SwipeActionTouchListener(mListView,this); + public SwipeActionAdapter setListView(ListView listView){ + this.mListView = listView; + mTouchListener = new SwipeActionTouchListener(listView,this); this.mListView.setOnTouchListener(mTouchListener); this.mListView.setOnScrollListener(mTouchListener.makeScrollListener()); this.mListView.setClipChildren(false); mTouchListener.setFadeOut(mFadeOut); + mTouchListener.setDimBackgrounds(mDimBackgrounds); mTouchListener.setFixedBackgrounds(mFixedBackgrounds); + mTouchListener.setNormalSwipeFraction(mNormalSwipeFraction); + mTouchListener.setFarSwipeFraction(mFarSwipeFraction); return this; } diff --git a/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeActionTouchListener.java b/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeActionTouchListener.java index 5c9249a..a3edc56 100644 --- a/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeActionTouchListener.java +++ b/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeActionTouchListener.java @@ -83,6 +83,8 @@ public class SwipeActionTouchListener implements View.OnTouchListener { private int mViewWidth = 1; // 1 and not 0 to prevent dividing by zero private boolean mFadeOut = false; private boolean mFixedBackgrounds = false; + private boolean mDimBackgrounds = false; + private float mNormalSwipeFraction = 0.25f; private float mFarSwipeFraction = 0.5f; // Transient properties @@ -207,15 +209,34 @@ protected void setFixedBackgrounds(boolean fixedBackgrounds){ mFixedBackgrounds = fixedBackgrounds; } + /** + * Set whether the backgrounds should be dimmed while in no-trigger zone + * The default value for this property is false: backgrounds will not dim + * + * @param dimBackgrounds true for fixed backgrounds, false for swipe in + */ + protected void setDimBackgrounds(boolean dimBackgrounds){ + mDimBackgrounds = dimBackgrounds; + } + /** * Set the fraction of the View Width that needs to be swiped before it is counted as a far swipe * - * @param farSwipeFraction float between 0 and 1 + * @param farSwipeFraction float between 0 and 1, should be equal to or greater than normalSwipeFraction */ protected void setFarSwipeFraction(float farSwipeFraction) { mFarSwipeFraction = farSwipeFraction; } + /** + * Set the fraction of the View Width that needs to be swiped before it is counted as a normal swipe + * + * @param normalSwipeFraction float between 0 and 1, should be equal to or less than farSwipeFraction + */ + protected void setNormalSwipeFraction(float normalSwipeFraction) { + mNormalSwipeFraction = normalSwipeFraction; + } + @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (mViewWidth < 2) { @@ -306,7 +327,7 @@ public boolean onTouch(View view, MotionEvent motionEvent) { float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); boolean dismiss = false; boolean dismissRight = false; - if (Math.abs(deltaX) > mViewWidth / 2 && mSwiping) { + if (Math.abs(deltaX) > (mViewWidth * mNormalSwipeFraction) && mSwiping) { dismiss = true; dismissRight = deltaX > 0; } else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity @@ -385,7 +406,7 @@ public void onAnimationEnd(Animator animation) { if(!mFar && Math.abs(deltaX) > mViewWidth*mFarSwipeFraction) mFar = true; if(!mFar) mDirection = (deltaX > 0 ? SwipeDirections.DIRECTION_NORMAL_RIGHT : SwipeDirections.DIRECTION_NORMAL_LEFT); else mDirection = (deltaX > 0 ? SwipeDirections.DIRECTION_FAR_RIGHT : SwipeDirections.DIRECTION_FAR_LEFT); - mDownViewGroup.showBackground(mDirection); + mDownViewGroup.showBackground(mDirection, mDimBackgrounds && (Math.abs(deltaX) < mViewWidth*mNormalSwipeFraction)); mDownView.setTranslationX(deltaX - mSwipingSlop); if(mFadeOut) mDownView.setAlpha(Math.max(0f, Math.min(1f, diff --git a/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeViewGroup.java b/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeViewGroup.java index b0f1d53..b8fa641 100644 --- a/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeViewGroup.java +++ b/library/src/main/java/com/wdullaer/swipeactionadapter/SwipeViewGroup.java @@ -87,13 +87,15 @@ public SwipeViewGroup addBackground(View background, int direction){ * Show the View linked to a key. Don't do anything if the key is not found * * @param direction The key of the View to be shown + * @param dimBackground Indicates whether the background should be dimmed */ - public void showBackground(int direction){ + public void showBackground(int direction, boolean dimBackground){ if(mBackgroundMap.get(direction) == null) return; if(visibleView != SwipeDirections.DIRECTION_NEUTRAL) mBackgroundMap.get(visibleView).setVisibility(View.INVISIBLE); mBackgroundMap.get(direction).setVisibility(View.VISIBLE); + mBackgroundMap.get(direction).setAlpha(dimBackground ? 0.4f : 1); visibleView = direction; }