From 44c00be01a5e1666cfce83630f91d3301594ef0b Mon Sep 17 00:00:00 2001 From: Matt Robinson Date: Wed, 13 Jul 2016 14:55:32 -0400 Subject: [PATCH] Swappable returns a boolean for saying if the swap occured or not --- lib-core-slh/build.gradle | 2 +- .../com/nhaarman/listviewanimations/ArrayAdapter.java | 3 ++- .../listviewanimations/BaseAdapterDecorator.java | 4 +++- .../nhaarman/listviewanimations/util/Swappable.java | 2 +- .../itemmanipulation/dragdrop/DragAndDropHandler.java | 10 ++++++---- 5 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib-core-slh/build.gradle b/lib-core-slh/build.gradle index 3619c8f7..75ff67db 100644 --- a/lib-core-slh/build.gradle +++ b/lib-core-slh/build.gradle @@ -62,4 +62,4 @@ android { textOutput 'stdout' warningsAsErrors true } -} \ No newline at end of file +} diff --git a/lib-core/src/main/java/com/nhaarman/listviewanimations/ArrayAdapter.java b/lib-core/src/main/java/com/nhaarman/listviewanimations/ArrayAdapter.java index 56cb9dd3..0add7788 100644 --- a/lib-core/src/main/java/com/nhaarman/listviewanimations/ArrayAdapter.java +++ b/lib-core/src/main/java/com/nhaarman/listviewanimations/ArrayAdapter.java @@ -137,10 +137,11 @@ public T remove(final int location) { } @Override - public void swapItems(final int positionOne, final int positionTwo) { + public boolean swapItems(final int positionOne, final int positionTwo) { T firstItem = mItems.set(positionOne, getItem(positionTwo)); notifyDataSetChanged(); mItems.set(positionTwo, firstItem); + return true; } public void propagateNotifyDataSetChanged(@NonNull final BaseAdapter slavedAdapter) { diff --git a/lib-core/src/main/java/com/nhaarman/listviewanimations/BaseAdapterDecorator.java b/lib-core/src/main/java/com/nhaarman/listviewanimations/BaseAdapterDecorator.java index 5f8ae49a..1ce1e8d3 100644 --- a/lib-core/src/main/java/com/nhaarman/listviewanimations/BaseAdapterDecorator.java +++ b/lib-core/src/main/java/com/nhaarman/listviewanimations/BaseAdapterDecorator.java @@ -227,12 +227,14 @@ public Object[] getSections() { } @Override - public void swapItems(final int positionOne, final int positionTwo) { + public boolean swapItems(final int positionOne, final int positionTwo) { if (mDecoratedBaseAdapter instanceof Swappable) { ((Swappable) mDecoratedBaseAdapter).swapItems(positionOne, positionTwo); + return true; } else { Log.w("ListViewAnimations", "Warning: swapItems called on an adapter that does not implement Swappable!"); } + return false; } @Override diff --git a/lib-core/src/main/java/com/nhaarman/listviewanimations/util/Swappable.java b/lib-core/src/main/java/com/nhaarman/listviewanimations/util/Swappable.java index c9d639d9..2681028d 100644 --- a/lib-core/src/main/java/com/nhaarman/listviewanimations/util/Swappable.java +++ b/lib-core/src/main/java/com/nhaarman/listviewanimations/util/Swappable.java @@ -29,5 +29,5 @@ public interface Swappable { * @param positionOne First adapter position. * @param positionTwo Second adapter position. */ - void swapItems(int positionOne, int positionTwo); + boolean swapItems(int positionOne, int positionTwo); } \ No newline at end of file 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..5381ad2d 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 @@ -430,11 +430,13 @@ private void switchViews(final View switchView, final long switchId, final float final int switchViewPosition = mWrapper.getPositionForView(switchView); int mobileViewPosition = mWrapper.getPositionForView(mMobileView); - ((Swappable) mAdapter).swapItems(switchViewPosition - mWrapper.getHeaderViewsCount(), mobileViewPosition - mWrapper.getHeaderViewsCount()); - ((BaseAdapter) mAdapter).notifyDataSetChanged(); + boolean didSwap = ((Swappable) mAdapter).swapItems(switchViewPosition - mWrapper.getHeaderViewsCount(), mobileViewPosition - mWrapper.getHeaderViewsCount()); + if (didSwap) { + ((BaseAdapter) mAdapter).notifyDataSetChanged(); - mHoverDrawable.shift(switchView.getHeight()); - mSwitchViewAnimator.animateSwitchView(switchId, translationY); + mHoverDrawable.shift(switchView.getHeight()); + mSwitchViewAnimator.animateSwitchView(switchId, translationY); + } } /**