Skip to content
This repository was archived by the owner on Apr 26, 2023. It is now read-only.
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
* <p>
* 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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand All @@ -943,7 +980,6 @@ private boolean isSwipeDirectionValid(float deltaX) {
case END:
return rtlSign * deltaX > 0;
}

}

@Override
Expand Down