Skip to content

Commit

Permalink
Intoroduce RecyclerViewDragDropManager.OnItemDragEventListener
Browse files Browse the repository at this point in the history
  • Loading branch information
h6ah4i committed Jun 28, 2015
1 parent 5fe4b5b commit 9d2cd22
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,14 @@ protected boolean isDragging() {
return mDragDropManager.isDragging();
}

/*package*/ int getDraggingItemInitialPosition() {
return mDraggingItemInitialPosition;
}

/*package*/ int getDraggingItemCurrentPosition() {
return mDraggingItemCurrentPosition;
}

private static void safeUpdateFlags(RecyclerView.ViewHolder holder, int flags) {
if (!(holder instanceof DraggableItemViewHolder)) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,29 @@ public class RecyclerViewDragDropManager {

// ---

/**
* Used for listening item drag events
*/
public interface OnItemDragEventListener {
/**
* Callback method to be invoked when dragging is started.
*
* @param position The position of the item.
*/
void onDraggingStarted(int position);

/**
* Callback method to be invoked when dragging is finished.
*
* @param fromPosition Previous position of the item.
* @param toPosition New position of the item.
* @param result Indicates whether the dragging operation was succeeded.
*/
void onDraggingFinished(int fromPosition, int toPosition, boolean result);
}

// --

private static final int SCROLL_DIR_NONE = 0;
private static final int SCROLL_DIR_UP = (1 << 0);
private static final int SCROLL_DIR_DOWN = (1 << 1);
Expand Down Expand Up @@ -139,6 +162,7 @@ public class RecyclerViewDragDropManager {
private int mOrigOverScrollMode;
private ItemDraggableRange mDraggableRange;
private InternalHandler mHandler;
private OnItemDragEventListener mItemDragEventListener;

/**
* Constructor.
Expand Down Expand Up @@ -384,6 +408,19 @@ public Interpolator setSwapTargetTranslationInterpolator() {
return mSwapTargetTranslationInterpolator;
}

public OnItemDragEventListener getOnItemDragEventListener() {
return mItemDragEventListener;
}

/**
* Sets OnItemDragEventListener listener
*
* @param listener
*/
public void setOnItemDragEventListener(OnItemDragEventListener listener) {
mItemDragEventListener = listener;
}

/*package*/ boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent e) {
final int action = MotionEventCompat.getActionMasked(e);

Expand Down Expand Up @@ -545,6 +582,10 @@ private void startDragging(RecyclerView rv, MotionEvent e, RecyclerView.ViewHold
if (mEdgeEffectDecorator != null) {
mEdgeEffectDecorator.reorderToTop();
}

if (mItemDragEventListener != null) {
mItemDragEventListener.onDraggingStarted(mAdapter.getDraggingItemInitialPosition());
}
}

/**
Expand Down Expand Up @@ -633,14 +674,26 @@ private void finishDragging(boolean result) {
mGrabbedItemHeight = 0;


int draggingItemInitialPosition = RecyclerView.NO_POSITION;
int draggingItemCurrentPosition = RecyclerView.NO_POSITION;

// raise onDragItemFinished() event
if (mAdapter != null) {
draggingItemInitialPosition = mAdapter.getDraggingItemInitialPosition();
draggingItemCurrentPosition = mAdapter.getDraggingItemCurrentPosition();
mAdapter.onDragItemFinished(draggedItem, result);
}

// if (draggedItem != null) {
// draggedItem.setIsRecyclable(true);
// }

if (mItemDragEventListener != null) {
mItemDragEventListener.onDraggingFinished(
draggingItemInitialPosition,
draggingItemCurrentPosition,
result);
}
}

private boolean handleActionUpOrCancel(RecyclerView rv, MotionEvent e) {
Expand Down

0 comments on commit 9d2cd22

Please sign in to comment.