From e7a3603da8f851b04e987dac6dcfa44ee6ab0435 Mon Sep 17 00:00:00 2001 From: ochkarik05 Date: Sun, 5 Apr 2015 15:49:49 +0300 Subject: [PATCH 1/2] Added method onMovingCanceled(int originalPosition) toOnItemMovedListener --- .../itemmanipulation/dragdrop/DragAndDropHandler.java | 8 ++++++-- .../itemmanipulation/dragdrop/OnItemMovedListener.java | 8 ++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DragAndDropHandler.java b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DragAndDropHandler.java index 26c231ba..87bdb869 100644 --- a/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DragAndDropHandler.java +++ b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DragAndDropHandler.java @@ -458,8 +458,12 @@ private boolean handleUpEvent() { valueAnimator.start(); int newPosition = getPositionForId(mMobileItemId) - mWrapper.getHeaderViewsCount(); - if (mOriginalMobileItemPosition != newPosition && mOnItemMovedListener != null) { - mOnItemMovedListener.onItemMoved(mOriginalMobileItemPosition, newPosition); + if(mOnItemMovedListener != null){ + if (mOriginalMobileItemPosition != newPosition) { + mOnItemMovedListener.onItemMoved(mOriginalMobileItemPosition, newPosition); + }else{ + mOnItemMovedListener.onMovingCanceled(mOriginalMobileItemPosition); + } } return true; diff --git a/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovedListener.java b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovedListener.java index 23a54852..468d76d7 100644 --- a/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovedListener.java +++ b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovedListener.java @@ -28,4 +28,12 @@ public interface OnItemMovedListener { * @param newPosition the new position of the item that was dragged. */ void onItemMoved(int originalPosition, int newPosition); + + /** + * Called when an item that was dragged has been dropped without moving to new position. + * + * @param originalPosition the original position of the item that was dragged. + * + */ + void onMovingCanceled(int originalPosition); } \ No newline at end of file From 2df582aa12bcae4361c69bd1f831ed7b8674c473 Mon Sep 17 00:00:00 2001 From: ochkarik05 Date: Sun, 5 Apr 2015 17:18:06 +0300 Subject: [PATCH 2/2] Item's moving canceled functionality moved to its own listener for compatibility --- .../dragdrop/DragAndDropHandler.java | 32 ++++++++++++++++--- .../dragdrop/OnItemMovedListener.java | 7 ---- .../OnItemMovingCanceledListener.java | 17 ++++++++++ 3 files changed, 45 insertions(+), 11 deletions(-) create mode 100644 lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovingCanceledListener.java diff --git a/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DragAndDropHandler.java b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DragAndDropHandler.java index 87bdb869..b323e3fd 100644 --- a/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DragAndDropHandler.java +++ b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/DragAndDropHandler.java @@ -107,6 +107,14 @@ public class DragAndDropHandler implements TouchEventHandler { @Nullable private OnItemMovedListener mOnItemMovedListener; + + + /** + * The {@link OnItemMovingCanceledListener} that is notified of moved items. + */ + @Nullable + private OnItemMovingCanceledListener mOnItemMovingCanceledListener; + /** * The raw x coordinate of the down event. */ @@ -250,6 +258,13 @@ public void setOnItemMovedListener(@Nullable final OnItemMovedListener onItemMov mOnItemMovedListener = onItemMovedListener; } + + /** + * Sets the {@link com.nhaarman.listviewanimations.itemmanipulation.dragdrop.OnItemMovingCanceledListener} that is notified when user has dropped a dragging item in its original position. + */ + public void setOnItemMovingCanceledListener(@Nullable final OnItemMovingCanceledListener onItemMovingCanceledListener) { + mOnItemMovingCanceledListener = onItemMovingCanceledListener; + } @Override public boolean isInteracting() { return mMobileItemId != INVALID_ID; @@ -441,8 +456,13 @@ private void switchViews(final View switchView, final long switchId, final float * Handles the up event. *

* Animates the hover drawable to its final position, and finalizes our drag properties when the animation has finished. + * * Will also notify the {@link com.nhaarman.listviewanimations.itemmanipulation.dragdrop.OnItemMovedListener} set if applicable. * + * Will also notify the + * {@link com.nhaarman.listviewanimations.itemmanipulation.dragdrop.OnItemMovingCanceledListener} + * if user drop item in its original position. + * * @return {@code true} if the event was handled, {@code false} otherwise. */ private boolean handleUpEvent() { @@ -458,13 +478,17 @@ private boolean handleUpEvent() { valueAnimator.start(); int newPosition = getPositionForId(mMobileItemId) - mWrapper.getHeaderViewsCount(); - if(mOnItemMovedListener != null){ if (mOriginalMobileItemPosition != newPosition) { - mOnItemMovedListener.onItemMoved(mOriginalMobileItemPosition, newPosition); + + if(mOnItemMovedListener != null) { + mOnItemMovedListener.onItemMoved(mOriginalMobileItemPosition, newPosition); + } }else{ - mOnItemMovedListener.onMovingCanceled(mOriginalMobileItemPosition); + if(mOnItemMovingCanceledListener != null){ + mOnItemMovingCanceledListener.onItemMovingCanceled(mOriginalMobileItemPosition); + } + } - } return true; } diff --git a/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovedListener.java b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovedListener.java index 468d76d7..5ee22f18 100644 --- a/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovedListener.java +++ b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovedListener.java @@ -29,11 +29,4 @@ public interface OnItemMovedListener { */ void onItemMoved(int originalPosition, int newPosition); - /** - * Called when an item that was dragged has been dropped without moving to new position. - * - * @param originalPosition the original position of the item that was dragged. - * - */ - void onMovingCanceled(int originalPosition); } \ No newline at end of file diff --git a/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovingCanceledListener.java b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovingCanceledListener.java new file mode 100644 index 00000000..bd52b617 --- /dev/null +++ b/lib-manipulation/src/main/java/com/nhaarman/listviewanimations/itemmanipulation/dragdrop/OnItemMovingCanceledListener.java @@ -0,0 +1,17 @@ +package com.nhaarman.listviewanimations.itemmanipulation.dragdrop; + +/** + * An interface which provides a callback that is called when an item's moving has been canceled + * using the {@link com.nhaarman.listviewanimations.itemmanipulation.DynamicListView}. + * It usually happens when user drags item but then drop it back to its original position + */ +public interface OnItemMovingCanceledListener { + + /** + * Called when an item that was dragged has been dropped without moving to new position. + * + * @param originalPosition the original position of the item that was dragged. + * + */ + void onItemMovingCanceled(int originalPosition); +}