Skip to content

Commit 8fea955

Browse files
committed
Improve the release drag logic.
1 parent 8412d50 commit 8fea955

File tree

1 file changed

+50
-24
lines changed

1 file changed

+50
-24
lines changed

library/src/main/java/com/daimajia/swipe/SwipeLayout.java

Lines changed: 50 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,11 @@ public int clampViewPositionVertical(View child, int top, int dy) {
323323

324324
@Override
325325
public boolean tryCaptureView(View child, int pointerId) {
326-
return child == getSurfaceView() || getBottomViews().contains(child);
326+
boolean result = child == getSurfaceView() || getBottomViews().contains(child);
327+
if(result){
328+
isCloseBeforeDrag = getOpenStatus() == Status.Close;
329+
}
330+
return result;
327331
}
328332

329333
@Override
@@ -336,13 +340,14 @@ public int getViewVerticalDragRange(View child) {
336340
return mDragDistance;
337341
}
338342

343+
boolean isCloseBeforeDrag = true;
339344
@Override
340345
public void onViewReleased(View releasedChild, float xvel, float yvel) {
341346
super.onViewReleased(releasedChild, xvel, yvel);
342347
for (SwipeListener l : mSwipeListeners)
343348
l.onHandRelease(SwipeLayout.this, xvel, yvel);
344349
if (releasedChild == getSurfaceView()) {
345-
processSurfaceRelease(xvel, yvel);
350+
processSurfaceRelease(xvel, yvel, isCloseBeforeDrag);
346351
} else if (getBottomViews().contains(releasedChild)) {
347352
if (getShowMode() == ShowMode.PullOut) {
348353
processBottomPullOutRelease(xvel, yvel);
@@ -1284,37 +1289,58 @@ public Status getOpenStatus() {
12841289
return Status.Middle;
12851290
}
12861291

1292+
12871293
/**
12881294
* Process the surface release event.
12891295
*
1290-
* @param xvel
1291-
* @param yvel
1296+
* @param xvel xVelocity
1297+
* @param yvel yVelocity
1298+
* @param isCloseBeforeDragged the open state before drag
12921299
*/
1293-
private void processSurfaceRelease(float xvel, float yvel) {
1294-
if (xvel == 0 && getOpenStatus() == Status.Middle) close();
1295-
1296-
if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left
1297-
|| mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Right) {
1298-
if (xvel > 0) {
1299-
if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left)
1300-
open();
1300+
private void processSurfaceRelease(float xvel, float yvel, boolean isCloseBeforeDragged) {
1301+
float minVelocity = mDragHelper.getMinVelocity();
1302+
View surfaceView = getSurfaceView();
1303+
DragEdge currentDragEdge = null;
1304+
try {
1305+
currentDragEdge = mDragEdges.get(mCurrentDirectionIndex);
1306+
} catch (Exception e) {
1307+
e.printStackTrace();
1308+
}
1309+
if(currentDragEdge == null || surfaceView == null){
1310+
return;
1311+
}
1312+
float willOpenPercent = (isCloseBeforeDragged ? .3f : .7f);
1313+
if(currentDragEdge == DragEdge.Left){
1314+
if(xvel > minVelocity) open();
1315+
else if(xvel < -minVelocity) close();
1316+
else{
1317+
float openPercent = 1f * getSurfaceView().getLeft() / mDragDistance;
1318+
if(openPercent > willOpenPercent ) open();
13011319
else close();
13021320
}
1303-
if (xvel < 0) {
1304-
if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Left)
1305-
close();
1306-
else open();
1321+
}else if(currentDragEdge == DragEdge.Right){
1322+
if(xvel > minVelocity) close();
1323+
else if(xvel < -minVelocity) open();
1324+
else{
1325+
float openPercent = 1f * (-getSurfaceView().getLeft()) / mDragDistance;
1326+
if(openPercent > willOpenPercent ) open();
1327+
else close();
13071328
}
1308-
} else {
1309-
if (yvel > 0) {
1310-
if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top)
1311-
open();
1329+
}else if(currentDragEdge == DragEdge.Top){
1330+
if(yvel > minVelocity) open();
1331+
else if(yvel < -minVelocity) close();
1332+
else{
1333+
float openPercent = 1f * getSurfaceView().getTop() / mDragDistance;
1334+
if(openPercent > willOpenPercent ) open();
13121335
else close();
13131336
}
1314-
if (yvel < 0) {
1315-
if (mDragEdges.get(mCurrentDirectionIndex) == DragEdge.Top)
1316-
close();
1317-
else open();
1337+
}else if(currentDragEdge == DragEdge.Bottom){
1338+
if(yvel > minVelocity) close();
1339+
else if(yvel < -minVelocity) open();
1340+
else{
1341+
float openPercent = 1f * (-getSurfaceView().getTop()) / mDragDistance;
1342+
if(openPercent > willOpenPercent ) open();
1343+
else close();
13181344
}
13191345
}
13201346
}

0 commit comments

Comments
 (0)