Skip to content

Commit 6a1d999

Browse files
committed
added FLING_OR_SLIDE_REMOVE mode
1 parent ec55b6f commit 6a1d999

File tree

4 files changed

+192
-23
lines changed

4 files changed

+192
-23
lines changed

demo/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<item>Fling left</item>
6262
<item>Slide right</item>
6363
<item>Slide left</item>
64+
<item>Fling or Slide</item>
6465
</string-array>
6566
<string-array name="drag_init_mode_labels">
6667
<item>On down</item>

demo/src/com/mobeta/android/demodslv/TestBedDSLV.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ public class TestBedDSLV extends FragmentActivity implements
2727
private int mNumHeaders = 0;
2828
private int mNumFooters = 0;
2929

30-
private int mDragStartMode = DragSortController.ON_DOWN;
31-
private boolean mRemoveEnabled = false;
32-
private int mRemoveMode = DragSortController.FLING_RIGHT_REMOVE;
30+
private int mDragStartMode = DragSortController.ON_DRAG;
31+
private boolean mRemoveEnabled = true;
32+
private int mRemoveMode = DragSortController.FLING_OR_SLIDE_REMOVE;
3333
private boolean mSortEnabled = true;
3434
private boolean mDragEnabled = true;
3535

library/src/com/mobeta/android/dslv/DragSortController.java

Lines changed: 104 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.mobeta.android.dslv;
22

3+
import android.util.Log;
34
import android.view.GestureDetector;
45
import android.view.HapticFeedbackConstants;
56
import android.view.MotionEvent;
@@ -40,13 +41,15 @@ public class DragSortController extends SimpleFloatViewManager implements View.O
4041
public static final int FLING_LEFT_REMOVE = 2;
4142
public static final int SLIDE_RIGHT_REMOVE = 3;
4243
public static final int SLIDE_LEFT_REMOVE = 4;
44+
public static final int FLING_OR_SLIDE_REMOVE = 5;
4345

4446
/**
4547
* The current remove mode.
4648
*/
4749
private int mRemoveMode;
4850

4951
private boolean mRemoveEnabled = false;
52+
private boolean mIsRemoving = false;
5053

5154
private GestureDetector mDetector;
5255

@@ -57,6 +60,7 @@ public class DragSortController extends SimpleFloatViewManager implements View.O
5760
public static final int MISS = -1;
5861

5962
private int mHitPos = MISS;
63+
private int mRemoveHitPos = MISS;
6064

6165
private int mClickRemoveHitPos = MISS;
6266

@@ -79,6 +83,8 @@ public class DragSortController extends SimpleFloatViewManager implements View.O
7983
private int mClickRemoveId;
8084

8185
private DragSortListView mDslv;
86+
private int mPositionX;
87+
private boolean mCanDrag;
8288

8389

8490
/**
@@ -207,7 +213,7 @@ public void setClickRemoveId(int id) {
207213
public boolean startDrag(int position, int deltaX, int deltaY) {
208214

209215
int dragFlags = 0;
210-
if (mSortEnabled) {
216+
if (mSortEnabled && (!mIsRemoving || mRemoveMode != FLING_OR_SLIDE_REMOVE )) {
211217
dragFlags |= DragSortListView.DRAG_POS_Y | DragSortListView.DRAG_NEG_Y;
212218
//dragFlags |= DRAG_POS_Y; //for fun
213219
}
@@ -217,6 +223,10 @@ public boolean startDrag(int position, int deltaX, int deltaY) {
217223
} else if (mRemoveMode == FLING_LEFT_REMOVE) {
218224
dragFlags |= DragSortListView.DRAG_NEG_X;
219225
}
226+
else if( mIsRemoving && mRemoveMode == FLING_OR_SLIDE_REMOVE ){
227+
dragFlags |= DragSortListView.DRAG_POS_X;
228+
dragFlags |= DragSortListView.DRAG_NEG_X;
229+
}
220230
}
221231

222232
mDragging = mDslv.startDrag(position - mDslv.getHeaderViewsCount(), dragFlags, deltaX, deltaY);
@@ -230,7 +240,7 @@ public boolean onTouch(View v, MotionEvent ev) {
230240
}
231241

232242
mDetector.onTouchEvent(ev);
233-
if (mRemoveEnabled && mDragging && (mRemoveMode == FLING_RIGHT_REMOVE || mRemoveMode == FLING_LEFT_REMOVE)) {
243+
if (mRemoveEnabled && mDragging && (mRemoveMode == FLING_RIGHT_REMOVE || mRemoveMode == FLING_LEFT_REMOVE || mRemoveMode == FLING_OR_SLIDE_REMOVE )) {
234244
mFlingRemoveDetector.onTouchEvent(ev);
235245
}
236246

@@ -243,12 +253,24 @@ public boolean onTouch(View v, MotionEvent ev) {
243253
break;
244254
case MotionEvent.ACTION_UP:
245255
if (mRemoveEnabled) {
246-
final int x = (int) ev.getX();
247-
int thirdW = mDslv.getWidth() / 3;
248-
int twoThirdW = mDslv.getWidth() - thirdW;
249-
if ((mRemoveMode == SLIDE_RIGHT_REMOVE && x > twoThirdW) ||
250-
(mRemoveMode == SLIDE_LEFT_REMOVE && x < thirdW)) {
251-
mDslv.stopDrag(true);
256+
if (mIsRemoving && mRemoveMode == FLING_OR_SLIDE_REMOVE ) {
257+
int x = mPositionX;
258+
if( x < 0 )
259+
x = -x;
260+
int removePoint = mDslv.getWidth() / 3;
261+
if( x > removePoint ){
262+
mDslv.stopDragWithVelocity(true,0);
263+
}
264+
}
265+
else
266+
{
267+
final int x = (int) ev.getX();
268+
int thirdW = mDslv.getWidth() / 3;
269+
int twoThirdW = mDslv.getWidth() - thirdW;
270+
if ((mRemoveMode == SLIDE_RIGHT_REMOVE && x > twoThirdW) ||
271+
(mRemoveMode == SLIDE_LEFT_REMOVE && x < thirdW)) {
272+
mDslv.stopDrag(true);
273+
}
252274
}
253275
}
254276
case MotionEvent.ACTION_CANCEL:
@@ -266,6 +288,9 @@ public boolean onTouch(View v, MotionEvent ev) {
266288
public void onDragFloatView(View floatView, Point position, Point touch) {
267289

268290
if (mRemoveEnabled) {
291+
if( mIsRemoving ){
292+
mPositionX = position.x;
293+
}
269294
int x = touch.x;
270295
int y = touch.y;
271296

@@ -315,6 +340,9 @@ public void onDragFloatView(View floatView, Point position, Point touch) {
315340
public int startDragPosition(MotionEvent ev) {
316341
return dragHandleHitPosition(ev);
317342
}
343+
public int startRemovePosition(MotionEvent ev) {
344+
return removeHandleHitPosition(ev);
345+
}
318346

319347
/**
320348
* Checks for the touch of an item's drag handle (specified by
@@ -329,6 +357,9 @@ public int startDragPosition(MotionEvent ev) {
329357
public int dragHandleHitPosition(MotionEvent ev) {
330358
return viewIdHitPosition(ev, mDragHandleId);
331359
}
360+
public int removeHandleHitPosition(MotionEvent ev) {
361+
return viewIdHitPosition(ev, 0);
362+
}
332363

333364
public int viewIdHitPosition(MotionEvent ev, int id) {
334365
final int x = (int) ev.getX();
@@ -349,7 +380,7 @@ public int viewIdHitPosition(MotionEvent ev, int id) {
349380
final int rawY = (int) ev.getRawY();
350381

351382
//View dragBox = (View) item.getTag();
352-
View dragBox = (View) item.findViewById(id);
383+
View dragBox = id == 0 ? item : (View) item.findViewById(id);
353384
boolean dragHit = false;
354385
if (dragBox != null) {
355386
dragBox.getLocationOnScreen(mTempLoc);
@@ -376,9 +407,15 @@ public boolean onDown(MotionEvent ev) {
376407
}
377408

378409
mHitPos = startDragPosition(ev);
379-
if (mHitPos != MISS && mDragInitMode == ON_DOWN) {
410+
if (mHitPos != MISS && mDragInitMode == ON_DOWN && mRemoveMode!=FLING_OR_SLIDE_REMOVE) {
380411
startDrag(mHitPos, (int) ev.getX() - mItemX, (int) ev.getY() - mItemY);
381412
}
413+
if( mRemoveMode == FLING_OR_SLIDE_REMOVE )
414+
{
415+
mCanDrag = true;
416+
mPositionX = 0;
417+
mRemoveHitPos = startRemovePosition(ev);
418+
}
382419

383420
return true;
384421
}
@@ -387,7 +424,33 @@ public boolean onDown(MotionEvent ev) {
387424
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
388425
//Log.d("mobeta", "lift listener scrolled dX="+distanceX+" dY="+distanceY);
389426

390-
if (mHitPos != MISS && mDragInitMode == ON_DRAG && !mDragging) {
427+
428+
if( mRemoveMode==FLING_OR_SLIDE_REMOVE && mCanDrag && !mDragging && (mHitPos != MISS || mRemoveHitPos !=MISS )) {
429+
final int x1 = (int) e1.getX();
430+
final int y1 = (int) e1.getY();
431+
final int x2 = (int) e2.getX();
432+
final int y2 = (int) e2.getY();
433+
434+
boolean start = false;
435+
if (mHitPos != MISS && mSortEnabled) {
436+
start = Math.abs(y2 - y1) > mTouchSlop;
437+
if( start )
438+
mIsRemoving = false;
439+
}
440+
if (mRemoveEnabled && !start) {
441+
start = Math.abs(x2 - x1) > mTouchSlop;
442+
if( start )
443+
mIsRemoving = true;
444+
}
445+
if (start) {
446+
startDrag(mHitPos!=MISS ? mHitPos : mRemoveHitPos, x2 - mItemX, y2 - mItemY);
447+
}
448+
else if( Math.abs(y2-y1) > mTouchSlop)
449+
{
450+
mCanDrag = false;
451+
}
452+
}
453+
else if (mHitPos != MISS && mDragInitMode == ON_DRAG && !mDragging) {
391454
final int x1 = (int) e1.getX();
392455
final int y1 = (int) e1.getY();
393456
final int x2 = (int) e2.getX();
@@ -413,7 +476,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
413476
@Override
414477
public void onLongPress(MotionEvent e) {
415478
//Log.d("mobeta", "lift listener long pressed");
416-
if (mHitPos != MISS && mDragInitMode == ON_LONG_PRESS) {
479+
if (mHitPos != MISS && mDragInitMode == ON_LONG_PRESS && mRemoveMode!=FLING_OR_SLIDE_REMOVE) {
417480
mDslv.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);
418481
startDrag(mHitPos, mCurrX - mItemX, mCurrY - mItemY);
419482
}
@@ -448,7 +511,35 @@ public void onShowPress(MotionEvent ev) {
448511
public final boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
449512
//Log.d("mobeta", "on fling remove called");
450513
if (mRemoveEnabled) {
451-
switch (mRemoveMode) {
514+
switch (mRemoveMode) {
515+
case FLING_OR_SLIDE_REMOVE:
516+
if( mIsRemoving) {
517+
int w = mDslv.getWidth();
518+
int minPos = w/5;
519+
if (velocityX > mFlingSpeed )
520+
{
521+
if( mPositionX > -minPos )
522+
{
523+
mDslv.stopDragWithVelocity(true,velocityX);
524+
}
525+
else
526+
{
527+
mIsRemoving = false;
528+
}
529+
}
530+
else if (velocityX < -mFlingSpeed )
531+
{
532+
if( mPositionX < minPos )
533+
{
534+
mDslv.stopDragWithVelocity(true,velocityX);
535+
}
536+
else
537+
{
538+
mIsRemoving = false;
539+
}
540+
}
541+
}
542+
break;
452543
case FLING_RIGHT_REMOVE:
453544
if (velocityX > mFlingSpeed) {
454545
mDslv.stopDrag(true);

0 commit comments

Comments
 (0)