From ba77604d45449c9fbe9f1ed48c60d80f294594e1 Mon Sep 17 00:00:00 2001 From: Drew Heavner Date: Thu, 18 Jun 2015 17:36:48 -0400 Subject: [PATCH] all slider callback configurations are now updated with the new configuration parameters (velocity and distance thresholds) Updated example to be up-to-date with the support libs --- .../r0adkll/slidr/example/ViewerActivity.java | 8 +- .../src/main/res/layout/activity_viewer.xml | 2 +- .../com/r0adkll/slidr/widget/SliderPanel.java | 187 ++++++++++++++++-- 3 files changed, 174 insertions(+), 23 deletions(-) diff --git a/example/src/main/java/com/r0adkll/slidr/example/ViewerActivity.java b/example/src/main/java/com/r0adkll/slidr/example/ViewerActivity.java index 06f98dd..3de59c4 100644 --- a/example/src/main/java/com/r0adkll/slidr/example/ViewerActivity.java +++ b/example/src/main/java/com/r0adkll/slidr/example/ViewerActivity.java @@ -2,6 +2,7 @@ import android.os.Bundle; import android.support.v7.app.ActionBarActivity; +import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.widget.TextView; @@ -20,7 +21,7 @@ /** * Created by r0adkll on 1/11/15. */ -public class ViewerActivity extends ActionBarActivity { +public class ViewerActivity extends AppCompatActivity { public static final String EXTRA_OS = "extra_os_version"; @@ -52,10 +53,13 @@ protected void onCreate(Bundle savedInstanceState) { int secondary = getResources().getColor(R.color.accent); // Build the slidr config + int numPositions = SlidrPosition.values().length; + SlidrPosition position = SlidrPosition.values()[Utils.getRandom().nextInt(numPositions)]; + SlidrConfig config = new SlidrConfig.Builder() .primaryColor(primary) .secondaryColor(secondary) - .position(SlidrPosition.VERTICAL) + .position(position) .velocityThreshold(2400) .distanceThreshold(.25f) .touchSize(Utils.dpToPx(this, 32)) diff --git a/example/src/main/res/layout/activity_viewer.xml b/example/src/main/res/layout/activity_viewer.xml index 606d5c2..f15b68e 100644 --- a/example/src/main/res/layout/activity_viewer.xml +++ b/example/src/main/res/layout/activity_viewer.xml @@ -40,7 +40,7 @@ android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/transparent" - app:theme="@style/ActionBarThemeOverlay" + android:theme="@style/ActionBarThemeOverlay" app:popupTheme="@style/ActionBarPopupThemeOverlay" /> diff --git a/library/src/main/java/com/r0adkll/slidr/widget/SliderPanel.java b/library/src/main/java/com/r0adkll/slidr/widget/SliderPanel.java index 1fe31da..1b283eb 100644 --- a/library/src/main/java/com/r0adkll/slidr/widget/SliderPanel.java +++ b/library/src/main/java/com/r0adkll/slidr/widget/SliderPanel.java @@ -79,8 +79,6 @@ public void setOnPanelSlideListener(OnPanelSlideListener listener){ /** * Initialize the slider panel * - * TODO: Based on SlidrPosition configure the ViewDragHelper to the appropriate position. - * */ private void init(){ mScreenWidth = getResources().getDisplayMetrics().widthPixels; @@ -111,6 +109,10 @@ private void init(){ callback = mVerticalCallback; position = ViewDragHelper.EDGE_TOP | ViewDragHelper.EDGE_BOTTOM; break; + case HORIZONTAL: + callback = mHorizontalCallback; + position = ViewDragHelper.EDGE_LEFT | ViewDragHelper.EDGE_RIGHT; + break; default: callback = mLeftCallback; position = ViewDragHelper.EDGE_LEFT; @@ -234,11 +236,26 @@ public int getViewHorizontalDragRange(View child) { public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); - final int width = getWidth(); - float offset = width - releasedChild.getLeft(); - int left = xvel < 0 || xvel == 0 && offset > 0.5f ? 0 : mScreenWidth; + int left = releasedChild.getLeft(); + int settleLeft = 0; + int leftThreshold = (int) (getWidth() * mConfig.getDistanceThreshold()); + boolean isVerticalSwiping = Math.abs(yvel) > mConfig.getVelocityThreshold(); + + if(xvel > 0){ + + if(Math.abs(xvel) > mConfig.getVelocityThreshold() && !isVerticalSwiping){ + settleLeft = mScreenWidth; + }else if(left > leftThreshold){ + settleLeft = mScreenWidth; + } + + }else if(xvel == 0){ + if(left > leftThreshold){ + settleLeft = mScreenWidth; + } + } - mDragHelper.settleCapturedViewAt(left, releasedChild.getTop()); + mDragHelper.settleCapturedViewAt(settleLeft, releasedChild.getTop()); invalidate(); } @@ -303,11 +320,26 @@ public int getViewHorizontalDragRange(View child) { public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); - final int width = getWidth(); - float offset = width + releasedChild.getLeft(); - int left = xvel > 0 || xvel == 0 && offset < (mScreenWidth - 0.5f) ? 0 : -mScreenWidth; + int left = releasedChild.getLeft(); + int settleLeft = 0; + int leftThreshold = (int) (getWidth() * mConfig.getDistanceThreshold()); + boolean isVerticalSwiping = Math.abs(yvel) > mConfig.getVelocityThreshold(); - mDragHelper.settleCapturedViewAt(left, releasedChild.getTop()); + if(xvel < 0){ + + if(Math.abs(xvel) > mConfig.getVelocityThreshold() && !isVerticalSwiping){ + settleLeft = -mScreenWidth; + }else if(left < -leftThreshold){ + settleLeft = -mScreenWidth; + } + + }else if(xvel == 0){ + if(left < -leftThreshold){ + settleLeft = -mScreenWidth; + } + } + + mDragHelper.settleCapturedViewAt(settleLeft, releasedChild.getTop()); invalidate(); } @@ -370,11 +402,24 @@ public int getViewVerticalDragRange(View child) { public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); - final int height = getHeight(); - float offset = height - releasedChild.getTop(); - int top = yvel < 0 || yvel == 0 && offset > 0.5f ? 0 : mScreenHeight; + int top = releasedChild.getTop(); + int settleTop = 0; + int topThreshold = (int) (getHeight() * mConfig.getDistanceThreshold()); + boolean isSideSwiping = Math.abs(xvel) > mConfig.getVelocityThreshold(); + + if(yvel > 0){ + if(Math.abs(yvel) > mConfig.getVelocityThreshold() && !isSideSwiping){ + settleTop = mScreenHeight; + }else if(top > topThreshold){ + settleTop = mScreenHeight; + } + }else if(yvel == 0){ + if(top > topThreshold){ + settleTop = mScreenHeight; + } + } - mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), top); + mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), settleTop); invalidate(); } @@ -436,11 +481,24 @@ public int getViewVerticalDragRange(View child) { public void onViewReleased(View releasedChild, float xvel, float yvel) { super.onViewReleased(releasedChild, xvel, yvel); - final int height = getHeight(); - float offset = height - releasedChild.getTop(); - int top = yvel > 0 || yvel == 0 && offset < (mScreenHeight-0.5f) ? 0 : -mScreenHeight; + int top = releasedChild.getTop(); + int settleTop = 0; + int topThreshold = (int) (getHeight() * mConfig.getDistanceThreshold()); + boolean isSideSwiping = Math.abs(xvel) > mConfig.getVelocityThreshold(); + + if(yvel < 0){ + if(Math.abs(yvel) > mConfig.getVelocityThreshold() && !isSideSwiping){ + settleTop = -mScreenHeight; + }else if(top < -topThreshold){ + settleTop = -mScreenHeight; + } + }else if(yvel == 0){ + if(top < -topThreshold){ + settleTop = -mScreenHeight; + } + } - mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), top); + mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), settleTop); invalidate(); } @@ -534,8 +592,6 @@ public void onViewReleased(View releasedChild, float xvel, float yvel) { } - Log.d(SliderPanel.class.getName(), String.format("Drag view released [top: %d, velocity: (%f, %f), distanceThreshold: %d, velThreshold: %f]", top, xvel, yvel, topThreshold, mConfig.getVelocityThreshold())); - mDragHelper.settleCapturedViewAt(releasedChild.getLeft(), settleTop); invalidate(); } @@ -575,6 +631,97 @@ public void onViewDragStateChanged(int state) { } }; + /** + * The drag helper callbacks for dragging the slidr attachment in both horizontal directions + */ + private ViewDragHelper.Callback mHorizontalCallback = new ViewDragHelper.Callback() { + @Override + public boolean tryCaptureView(View child, int pointerId) { + return child.getId() == mDecorView.getId(); + } + + @Override + public int clampViewPositionHorizontal(View child, int left, int dx) { + return clamp(left, -mScreenWidth, mScreenWidth); + } + + @Override + public int getViewHorizontalDragRange(View child) { + return mScreenWidth; + } + + @Override + public void onViewReleased(View releasedChild, float xvel, float yvel) { + super.onViewReleased(releasedChild, xvel, yvel); + + int left = releasedChild.getLeft(); + int settleLeft = 0; + int leftThreshold = (int) (getWidth() * mConfig.getDistanceThreshold()); + boolean isVerticalSwiping = Math.abs(yvel) > mConfig.getVelocityThreshold(); + + if(xvel > 0){ + + if(Math.abs(xvel) > mConfig.getVelocityThreshold() && !isVerticalSwiping){ + settleLeft = mScreenWidth; + }else if(left > leftThreshold){ + settleLeft = mScreenWidth; + } + + }else if(xvel < 0){ + + if(Math.abs(xvel) > mConfig.getVelocityThreshold() && !isVerticalSwiping){ + settleLeft = -mScreenWidth; + }else if(left < -leftThreshold){ + settleLeft = -mScreenWidth; + } + + }else{ + if(left > leftThreshold){ + settleLeft = mScreenWidth; + }else if(left < -leftThreshold){ + settleLeft = -mScreenWidth; + } + } + + mDragHelper.settleCapturedViewAt(settleLeft, releasedChild.getTop()); + invalidate(); + } + + @Override + public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { + super.onViewPositionChanged(changedView, left, top, dx, dy); + float percent = 1f - ((float)Math.abs(left) / (float)mScreenWidth); + + if(mListener != null) mListener.onSlideChange(percent); + + // Update the dimmer alpha + applyScrim(percent); + } + + @Override + public void onViewDragStateChanged(int state) { + super.onViewDragStateChanged(state); + if(mListener != null) mListener.onStateChanged(state); + switch (state){ + case ViewDragHelper.STATE_IDLE: + if(mDecorView.getLeft() == 0){ + // State Open + if(mListener != null) mListener.onOpened(); + }else{ + // State Closed + if(mListener != null) mListener.onClosed(); + } + break; + case ViewDragHelper.STATE_DRAGGING: + + break; + case ViewDragHelper.STATE_SETTLING: + + break; + } + } + }; + /*********************************************************************************************** * * Helper Methods