1
1
package com .mobeta .android .dslv ;
2
2
3
+ import android .util .Log ;
3
4
import android .view .GestureDetector ;
4
5
import android .view .HapticFeedbackConstants ;
5
6
import android .view .MotionEvent ;
@@ -40,13 +41,15 @@ public class DragSortController extends SimpleFloatViewManager implements View.O
40
41
public static final int FLING_LEFT_REMOVE = 2 ;
41
42
public static final int SLIDE_RIGHT_REMOVE = 3 ;
42
43
public static final int SLIDE_LEFT_REMOVE = 4 ;
44
+ public static final int FLING_OR_SLIDE_REMOVE = 5 ;
43
45
44
46
/**
45
47
* The current remove mode.
46
48
*/
47
49
private int mRemoveMode ;
48
50
49
51
private boolean mRemoveEnabled = false ;
52
+ private boolean mIsRemoving = false ;
50
53
51
54
private GestureDetector mDetector ;
52
55
@@ -57,6 +60,7 @@ public class DragSortController extends SimpleFloatViewManager implements View.O
57
60
public static final int MISS = -1 ;
58
61
59
62
private int mHitPos = MISS ;
63
+ private int mRemoveHitPos = MISS ;
60
64
61
65
private int mClickRemoveHitPos = MISS ;
62
66
@@ -79,6 +83,8 @@ public class DragSortController extends SimpleFloatViewManager implements View.O
79
83
private int mClickRemoveId ;
80
84
81
85
private DragSortListView mDslv ;
86
+ private int mPositionX ;
87
+ private boolean mCanDrag ;
82
88
83
89
84
90
/**
@@ -207,7 +213,7 @@ public void setClickRemoveId(int id) {
207
213
public boolean startDrag (int position , int deltaX , int deltaY ) {
208
214
209
215
int dragFlags = 0 ;
210
- if (mSortEnabled ) {
216
+ if (mSortEnabled && (! mIsRemoving || mRemoveMode != FLING_OR_SLIDE_REMOVE ) ) {
211
217
dragFlags |= DragSortListView .DRAG_POS_Y | DragSortListView .DRAG_NEG_Y ;
212
218
//dragFlags |= DRAG_POS_Y; //for fun
213
219
}
@@ -217,6 +223,10 @@ public boolean startDrag(int position, int deltaX, int deltaY) {
217
223
} else if (mRemoveMode == FLING_LEFT_REMOVE ) {
218
224
dragFlags |= DragSortListView .DRAG_NEG_X ;
219
225
}
226
+ else if ( mIsRemoving && mRemoveMode == FLING_OR_SLIDE_REMOVE ){
227
+ dragFlags |= DragSortListView .DRAG_POS_X ;
228
+ dragFlags |= DragSortListView .DRAG_NEG_X ;
229
+ }
220
230
}
221
231
222
232
mDragging = mDslv .startDrag (position - mDslv .getHeaderViewsCount (), dragFlags , deltaX , deltaY );
@@ -230,7 +240,7 @@ public boolean onTouch(View v, MotionEvent ev) {
230
240
}
231
241
232
242
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 )) {
234
244
mFlingRemoveDetector .onTouchEvent (ev );
235
245
}
236
246
@@ -243,12 +253,24 @@ public boolean onTouch(View v, MotionEvent ev) {
243
253
break ;
244
254
case MotionEvent .ACTION_UP :
245
255
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
+ }
252
274
}
253
275
}
254
276
case MotionEvent .ACTION_CANCEL :
@@ -266,6 +288,9 @@ public boolean onTouch(View v, MotionEvent ev) {
266
288
public void onDragFloatView (View floatView , Point position , Point touch ) {
267
289
268
290
if (mRemoveEnabled ) {
291
+ if ( mIsRemoving ){
292
+ mPositionX = position .x ;
293
+ }
269
294
int x = touch .x ;
270
295
int y = touch .y ;
271
296
@@ -315,6 +340,9 @@ public void onDragFloatView(View floatView, Point position, Point touch) {
315
340
public int startDragPosition (MotionEvent ev ) {
316
341
return dragHandleHitPosition (ev );
317
342
}
343
+ public int startRemovePosition (MotionEvent ev ) {
344
+ return removeHandleHitPosition (ev );
345
+ }
318
346
319
347
/**
320
348
* Checks for the touch of an item's drag handle (specified by
@@ -329,6 +357,9 @@ public int startDragPosition(MotionEvent ev) {
329
357
public int dragHandleHitPosition (MotionEvent ev ) {
330
358
return viewIdHitPosition (ev , mDragHandleId );
331
359
}
360
+ public int removeHandleHitPosition (MotionEvent ev ) {
361
+ return viewIdHitPosition (ev , 0 );
362
+ }
332
363
333
364
public int viewIdHitPosition (MotionEvent ev , int id ) {
334
365
final int x = (int ) ev .getX ();
@@ -349,7 +380,7 @@ public int viewIdHitPosition(MotionEvent ev, int id) {
349
380
final int rawY = (int ) ev .getRawY ();
350
381
351
382
//View dragBox = (View) item.getTag();
352
- View dragBox = (View ) item .findViewById (id );
383
+ View dragBox = id == 0 ? item : (View ) item .findViewById (id );
353
384
boolean dragHit = false ;
354
385
if (dragBox != null ) {
355
386
dragBox .getLocationOnScreen (mTempLoc );
@@ -376,9 +407,15 @@ public boolean onDown(MotionEvent ev) {
376
407
}
377
408
378
409
mHitPos = startDragPosition (ev );
379
- if (mHitPos != MISS && mDragInitMode == ON_DOWN ) {
410
+ if (mHitPos != MISS && mDragInitMode == ON_DOWN && mRemoveMode != FLING_OR_SLIDE_REMOVE ) {
380
411
startDrag (mHitPos , (int ) ev .getX () - mItemX , (int ) ev .getY () - mItemY );
381
412
}
413
+ if ( mRemoveMode == FLING_OR_SLIDE_REMOVE )
414
+ {
415
+ mCanDrag = true ;
416
+ mPositionX = 0 ;
417
+ mRemoveHitPos = startRemovePosition (ev );
418
+ }
382
419
383
420
return true ;
384
421
}
@@ -387,7 +424,33 @@ public boolean onDown(MotionEvent ev) {
387
424
public boolean onScroll (MotionEvent e1 , MotionEvent e2 , float distanceX , float distanceY ) {
388
425
//Log.d("mobeta", "lift listener scrolled dX="+distanceX+" dY="+distanceY);
389
426
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 ) {
391
454
final int x1 = (int ) e1 .getX ();
392
455
final int y1 = (int ) e1 .getY ();
393
456
final int x2 = (int ) e2 .getX ();
@@ -413,7 +476,7 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float d
413
476
@ Override
414
477
public void onLongPress (MotionEvent e ) {
415
478
//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 ) {
417
480
mDslv .performHapticFeedback (HapticFeedbackConstants .LONG_PRESS );
418
481
startDrag (mHitPos , mCurrX - mItemX , mCurrY - mItemY );
419
482
}
@@ -448,7 +511,35 @@ public void onShowPress(MotionEvent ev) {
448
511
public final boolean onFling (MotionEvent e1 , MotionEvent e2 , float velocityX , float velocityY ) {
449
512
//Log.d("mobeta", "on fling remove called");
450
513
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 ;
452
543
case FLING_RIGHT_REMOVE :
453
544
if (velocityX > mFlingSpeed ) {
454
545
mDslv .stopDrag (true );
0 commit comments