diff --git a/EnhancedListView/src/main/java/de/timroes/android/listview/EnhancedListView.java b/EnhancedListView/src/main/java/de/timroes/android/listview/EnhancedListView.java index 85ae61a..5ba1213 100644 --- a/EnhancedListView/src/main/java/de/timroes/android/listview/EnhancedListView.java +++ b/EnhancedListView/src/main/java/de/timroes/android/listview/EnhancedListView.java @@ -123,6 +123,27 @@ public enum SwipeDirection { } + /** + * The callback interface used by {@link #setSwipeDirectionCallback(EnhancedListView.OnSwipeDirectionCallback)} + * to detect if the item swipe direction is valid. If the callback is set, the global + * swiping direction flag is overridden. Implement this to implement a item-dependent + * swipe direction. + */ + public interface OnSwipeDirectionCallback { + + /** + * Called when the user is swiping an item from the list. + *
+ * Return the type of swipe the item supports. + * + * @param listView The {@link EnhancedListView} the item is wiping from. + * @param position The position of the item to swipe in your adapter. + * @return The support item {@link SwipeDirection}. + */ + SwipeDirection onSwipeDirection(EnhancedListView listView, int position); + + } + /** * The callback interface used by {@link #setShouldSwipeCallback(EnhancedListView.OnShouldSwipeCallback)} * to inform its client that a list item is going to be swiped and check whether is @@ -313,6 +334,7 @@ public void handleMessage(Message msg) { // Swipe-To-Dismiss private boolean mSwipeEnabled; private OnDismissCallback mDismissCallback; + private OnSwipeDirectionCallback mSwipeDirectionCallback; private OnShouldSwipeCallback mShouldSwipeCallback; private UndoStyle mUndoStyle = UndoStyle.SINGLE_POPUP; private boolean mTouchBeforeAutoHide = true; @@ -466,6 +488,17 @@ public EnhancedListView setShouldSwipeCallback(OnShouldSwipeCallback shouldSwipe return this; } + /** + * Sets the callback to be called when the user is swiping an item from the list. + * + * @param swipeDirectionCallback The callback used to handle swipe-directions of list items. + * @return This {@link de.timroes.android.listview.EnhancedListView} + */ + public EnhancedListView setSwipeDirectionCallback(OnSwipeDirectionCallback swipeDirectionCallback) { + mSwipeDirectionCallback = swipeDirectionCallback; + return this; + } + /** * Sets the undo style of this list. See the javadoc of {@link de.timroes.android.listview.EnhancedListView.UndoStyle} * for a detailed explanation of the different styles. The default style (if you never call this @@ -682,11 +715,11 @@ public boolean onTouchEvent(MotionEvent ev) { int position = getPositionForView(mSwipeDownView) - getHeaderViewsCount(); if ((mShouldSwipeCallback == null) || mShouldSwipeCallback.onShouldSwipe(this, position)) { - mDownX = ev.getRawX(); + mDownX = ev.getRawX(); mDownPosition = position; - mVelocityTracker = VelocityTracker.obtain(); - mVelocityTracker.addMovement(ev); + mVelocityTracker = VelocityTracker.obtain(); + mVelocityTracker.addMovement(ev); } else { // set back to null to revert swiping mSwipeDownView = mSwipeDownChild = null; @@ -924,7 +957,6 @@ public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCoun * @return Whether the delta of a swipe is in the right direction. */ private boolean isSwipeDirectionValid(float deltaX) { - int rtlSign = 1; // On API level 17 and above, check if we are in a Right-To-Left layout if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { @@ -934,7 +966,12 @@ private boolean isSwipeDirectionValid(float deltaX) { } // Check if swipe has been done in the correct direction - switch(mSwipeDirection) { + SwipeDirection swipeDirection = mSwipeDirection; + if (mSwipeDirectionCallback != null) { + swipeDirection = mSwipeDirectionCallback.onSwipeDirection(this, mDownPosition); + } + + switch (swipeDirection) { default: case BOTH: return true; @@ -943,7 +980,6 @@ private boolean isSwipeDirectionValid(float deltaX) { case END: return rtlSign * deltaX > 0; } - } @Override