Skip to content

Commit

Permalink
all slider callback configurations are now updated with the new confi…
Browse files Browse the repository at this point in the history
…guration parameters (velocity and distance thresholds)

Updated example to be up-to-date with the support libs
  • Loading branch information
r0adkll committed Jun 18, 2015
1 parent bd1c5a4 commit ba77604
Show file tree
Hide file tree
Showing 3 changed files with 174 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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";

Expand Down Expand Up @@ -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))
Expand Down
2 changes: 1 addition & 1 deletion example/src/main/res/layout/activity_viewer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
/>

Expand Down
187 changes: 167 additions & 20 deletions library/src/main/java/com/r0adkll/slidr/widget/SliderPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit ba77604

Please sign in to comment.