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..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,9 +478,17 @@ private boolean handleUpEvent() { valueAnimator.start(); int newPosition = getPositionForId(mMobileItemId) - mWrapper.getHeaderViewsCount(); - if (mOriginalMobileItemPosition != newPosition && mOnItemMovedListener != null) { - mOnItemMovedListener.onItemMoved(mOriginalMobileItemPosition, newPosition); - } + if (mOriginalMobileItemPosition != newPosition) { + + if(mOnItemMovedListener != null) { + mOnItemMovedListener.onItemMoved(mOriginalMobileItemPosition, newPosition); + } + }else{ + 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 23a54852..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 @@ -28,4 +28,5 @@ public interface OnItemMovedListener { * @param newPosition the new position of the item that was dragged. */ void onItemMoved(int originalPosition, int newPosition); + } \ 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); +}