Skip to content

Commit

Permalink
[BREAKING CHANGE] Introduce onItemDragStarted()/onItemDragFinished() …
Browse files Browse the repository at this point in the history
…callbacks and reduce internal notifyDatasetChanged() method calls
  • Loading branch information
h6ah4i committed Oct 9, 2017
1 parent fc256af commit 84d5818
Show file tree
Hide file tree
Showing 17 changed files with 298 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}

@Override
public void onItemDragStarted(int position) {
notifyDataSetChanged();
}

@Override
public void onItemDragFinished(int fromPosition, int toPosition, boolean result) {
notifyDataSetChanged();
}

@Override
public void onClick(View v) {
RecyclerView rv = RecyclerViewAdapterUtils.getParentRecyclerView(v);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,14 @@ public ItemDraggableRange onGetItemDraggableRange(MyViewHolder holder, int posit
public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}

@Override
public void onItemDragStarted(int position) {
notifyDataSetChanged();
}

@Override
public void onItemDragFinished(int fromPosition, int toPosition, boolean result) {
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,14 @@ public ItemDraggableRange onGetItemDraggableRange(MyViewHolder holder, int posit
public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return (draggingPosition % 2) == (dropPosition % 2);
}

@Override
public void onItemDragStarted(int position) {
notifyDataSetChanged();
}

@Override
public void onItemDragFinished(int fromPosition, int toPosition, boolean result) {
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,14 @@ public ItemDraggableRange onGetItemDraggableRange(MyViewHolder holder, int posit
public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}

@Override
public void onItemDragStarted(int position) {
notifyDataSetChanged();
}

@Override
public void onItemDragFinished(int fromPosition, int toPosition, boolean result) {
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,13 @@ public ItemDraggableRange onGetItemDraggableRange(MyViewHolder holder, int posit
public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}

@Override
public void onItemDragStarted(int position) {
}

@Override
public void onItemDragFinished(int fromPosition, int toPosition, boolean result) {
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,14 @@ public ItemDraggableRange onGetItemDraggableRange(MyViewHolder holder, int posit
public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}

@Override
public void onItemDragStarted(int position) {
notifyDataSetChanged();
}

@Override
public void onItemDragFinished(int fromPosition, int toPosition, boolean result) {
notifyDataSetChanged();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,16 @@ public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}

@Override
public void onItemDragStarted(int position) {
notifyDataSetChanged();
}

@Override
public void onItemDragFinished(int fromPosition, int toPosition, boolean result) {
notifyDataSetChanged();
}

static int getHeaderItemCount() {
return (USE_DUMMY_HEADER) ? 1 : 0;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}

@Override
public void onItemDragStarted(int position) {
notifyDataSetChanged();
}

@Override
public void onItemDragFinished(int fromPosition, int toPosition, boolean result) {
notifyDataSetChanged();
}

private int findFirstSectionItem(int position) {
AbstractDataProvider.Data item = mProvider.getItem(position);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,16 @@ public boolean onCheckCanDrop(int draggingPosition, int dropPosition) {
return true;
}

@Override
public void onItemDragStarted(int position) {
notifyDataSetChanged();
}

@Override
public void onItemDragFinished(int fromPosition, int toPosition, boolean result) {
notifyDataSetChanged();
}

@Override
public int onGetSwipeReactionType(MyViewHolder holder, int position, int x, int y) {
if (onCheckCanStartDrag(holder, position, x, y)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,7 @@
import android.widget.TextView;

import com.h6ah4i.android.example.advrecyclerview.R;
import com.h6ah4i.android.widget.advrecyclerview.draggable.DraggableItemAdapter;
import com.h6ah4i.android.widget.advrecyclerview.draggable.ItemDraggableRange;
import com.h6ah4i.android.widget.advrecyclerview.expandable.RecyclerViewExpandableItemManager;
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractDraggableItemViewHolder;
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractExpandableItemAdapter;
import com.h6ah4i.android.widget.advrecyclerview.utils.AbstractExpandableItemViewHolder;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,26 @@ public void onMoveChildItem(int fromGroupPosition, int fromChildPosition, int to
mProvider.moveChildItem(fromGroupPosition, fromChildPosition, toGroupPosition, toChildPosition);
}

@Override
public void onGroupDragStarted(int groupPosition) {
notifyDataSetChanged();
}

@Override
public void onChildDragStarted(int groupPosition, int childPosition) {
notifyDataSetChanged();
}

@Override
public void onGroupDragFinished(int fromGroupPosition, int toGroupPosition, boolean result) {
notifyDataSetChanged();
}

@Override
public void onChildDragFinished(int fromGroupPosition, int fromChildPosition, int toGroupPosition, int toChildPosition, boolean result) {
notifyDataSetChanged();
}

private int findFirstSectionItem(int position) {
AbstractExpandableDataProvider.GroupData item = mProvider.getGroupItem(position);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,26 @@ public void onMoveChildItem(int fromGroupPosition, int fromChildPosition, int to
mProvider.moveChildItem(fromGroupPosition, fromChildPosition, toGroupPosition, toChildPosition);
}

@Override
public void onGroupDragStarted(int groupPosition) {
notifyDataSetChanged();
}

@Override
public void onChildDragStarted(int groupPosition, int childPosition) {
notifyDataSetChanged();
}

@Override
public void onGroupDragFinished(int fromGroupPosition, int toGroupPosition, boolean result) {
notifyDataSetChanged();
}

@Override
public void onChildDragFinished(int fromGroupPosition, int fromChildPosition, int toGroupPosition, int toChildPosition, boolean result) {
notifyDataSetChanged();
}

@Override
public int onGetGroupItemSwipeReactionType(MyGroupViewHolder holder, int groupPosition, int x, int y) {
if (onCheckGroupCanStartDrag(holder, groupPosition, x, y)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,24 @@ public interface DraggableItemAdapter<T extends RecyclerView.ViewHolder> {
* @return Whether can be dropped to the specified position.
*/
boolean onCheckCanDrop(int draggingPosition, int dropPosition);

/**
* Callback method to be invoked when started dragging an item.
*
* Call the {@link RecyclerView.Adapter#notifyDataSetChanged()} method in this callback to get the same behavior with v0.10.x or before.
*
* @param position The position of the item.
*/
void onItemDragStarted(int position);

/**
* Callback method to be invoked when finished dragging an item.
*
* Call the {@link RecyclerView.Adapter#notifyDataSetChanged()} method in this callback to get the same behavior with v0.10.x or before.
*
* @param fromPosition Previous position of the item.
* @param toPosition New position of the item.
* @param result Indicates whether the dragging operation was succeeded.
*/
void onItemDragFinished(int fromPosition, int toPosition, boolean result);
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ private interface Constants extends DraggableItemConstants {
private int mDraggingItemInitialPosition = RecyclerView.NO_POSITION;
private int mDraggingItemCurrentPosition = RecyclerView.NO_POSITION;
private int mItemMoveMode;
private boolean mCallingDragStarted;

public DraggableItemWrapperAdapter(RecyclerViewDragDropManager manager, RecyclerView.Adapter<VH> adapter) {
super(adapter);
Expand Down Expand Up @@ -223,7 +224,7 @@ private boolean shouldCancelDragOnDataUpdated() {
if (DEBUG_BYPASS_MOVE_OPERATION_MODE) {
return false;
}
return isDragging();
return isDragging() && !mCallingDragStarted;
}

private void cancelDrag() {
Expand All @@ -233,7 +234,7 @@ private void cancelDrag() {
}

// NOTE: This method is called from RecyclerViewDragDropManager
/*package*/ void onDragItemStarted(DraggingItemInfo draggingItemInfo, RecyclerView.ViewHolder holder, ItemDraggableRange range, int wrappedItemPosition, int itemMoveMode) {
/*package*/ void startDraggingItem(DraggingItemInfo draggingItemInfo, RecyclerView.ViewHolder holder, ItemDraggableRange range, int wrappedItemPosition, int itemMoveMode) {
if (LOCAL_LOGD) {
Log.d(TAG, "onDragItemStarted(holder = " + holder + ")");
}
Expand All @@ -257,24 +258,28 @@ private void cancelDrag() {
mDraggingItemViewHolder = holder;
mDraggableRange = range;
mItemMoveMode = itemMoveMode;
}

notifyDataSetChanged();
// NOTE: This method is called from RecyclerViewDragDropManager
/*package*/ void onDragItemStarted() {
mCallingDragStarted = true;
mDraggableItemAdapter.onItemDragStarted(getDraggingItemInitialPosition());
mCallingDragStarted = false;
}

// NOTE: This method is called from RecyclerViewDragDropManager
/*package*/ void onDragItemFinished(boolean result) {
/*package*/ void onDragItemFinished(
int draggingItemInitialPosition, int draggingItemCurrentPosition, boolean result) {
if (LOCAL_LOGD) {
Log.d(TAG, "onDragItemFinished(result = " + result + ")");
Log.d(TAG, "onDragItemFinished(draggingItemInitialPosition = " + draggingItemInitialPosition +
", draggingItemCurrentPosition = " + draggingItemCurrentPosition + ", result = " + result + ")");
}

if (DEBUG_BYPASS_MOVE_OPERATION_MODE) {
return;
}

if (result && (mDraggingItemCurrentPosition != mDraggingItemInitialPosition)) {
// apply to wrapped adapter
mDraggableItemAdapter.onMoveItem(mDraggingItemInitialPosition, mDraggingItemCurrentPosition);
}
DraggableItemAdapter draggableItemAdapter = mDraggableItemAdapter;

mDraggingItemInitialPosition = RecyclerView.NO_POSITION;
mDraggingItemCurrentPosition = RecyclerView.NO_POSITION;
Expand All @@ -283,7 +288,12 @@ private void cancelDrag() {
mDraggingItemViewHolder = null;
mDraggableItemAdapter = null;

notifyDataSetChanged();
if (result && (draggingItemCurrentPosition != draggingItemInitialPosition)) {
// apply to wrapped adapter
draggableItemAdapter.onMoveItem(draggingItemInitialPosition, draggingItemCurrentPosition);
}

draggableItemAdapter.onItemDragFinished(draggingItemInitialPosition, draggingItemCurrentPosition, result);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -762,8 +762,7 @@ private void startDragging(RecyclerView rv, MotionEvent e, RecyclerView.ViewHold

startScrollOnDraggingProcess();

// raise onDragItemStarted() event
mWrapperAdapter.onDragItemStarted(mDraggingItemInfo, holder, mDraggableRange, wrappedItemPosition, mCurrentItemMoveMode);
mWrapperAdapter.startDraggingItem(mDraggingItemInfo, holder, mDraggableRange, wrappedItemPosition, mCurrentItemMoveMode);

// setup decorators
mWrapperAdapter.onBindViewHolder(holder, wrappedItemPosition);
Expand All @@ -786,6 +785,9 @@ private void startDragging(RecyclerView rv, MotionEvent e, RecyclerView.ViewHold
mEdgeEffectDecorator.reorderToTop();
}

// raise onDragItemStarted() event
mWrapperAdapter.onDragItemStarted();

if (mItemDragEventListener != null) {
mItemDragEventListener.onItemDragStarted(mWrapperAdapter.getDraggingItemInitialPosition());
mItemDragEventListener.onItemDragMoveDistanceUpdated(0, 0);
Expand Down Expand Up @@ -905,7 +907,10 @@ private void finishDragging(boolean result) {
if (mWrapperAdapter != null) {
draggingItemInitialPosition = mWrapperAdapter.getDraggingItemInitialPosition();
draggingItemCurrentPosition = mWrapperAdapter.getDraggingItemCurrentPosition();
mWrapperAdapter.onDragItemFinished(result);
mWrapperAdapter.onDragItemFinished(
draggingItemInitialPosition,
draggingItemCurrentPosition,
result);
}

// if (draggedItem != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,47 @@ public interface ExpandableDraggableItemAdapter<GVH extends RecyclerView.ViewHol
* @return Whether can be dropped to the specified position.
*/
boolean onCheckChildCanDrop(int draggingGroupPosition, int draggingChildPosition, int dropGroupPosition, int dropChildPosition);

/**
* Callback method to be invoked when started dragging a group item.
*
* Call the {@link RecyclerView.Adapter#notifyDataSetChanged()} method in this callback to get the same behavior with v0.10.x or before.
*
* @param groupPosition The position of the group item.
*/
void onGroupDragStarted(int groupPosition);

/**
* Callback method to be invoked when started dragging a child item.
*
* Call the {@link RecyclerView.Adapter#notifyDataSetChanged()} method in this callback to get the same behavior with v0.10.x or before.
*
* @param groupPosition The group position of the item.
* @param childPosition The child position of the item.
*/
void onChildDragStarted(int groupPosition, int childPosition);

/**
* Callback method to be invoked when finished dragging a group item.
*
* Call the {@link RecyclerView.Adapter#notifyDataSetChanged()} method in this callback to get the same behavior with v0.10.x or before.
*
* @param fromGroupPosition Previous position of the group item.
* @param toGroupPosition New position of the group item.
* @param result Indicates whether the dragging operation was succeeded.
*/
void onGroupDragFinished(int fromGroupPosition, int toGroupPosition, boolean result);

/**
* Callback method to be invoked when finished dragging a child item.
*
* Call the {@link RecyclerView.Adapter#notifyDataSetChanged()} method in this callback to get the same behavior with v0.10.x or before.
*
* @param fromGroupPosition Previous group position of the item.
* @param fromChildPosition Previous child position of the item.
* @param toGroupPosition New group position of the item.
* @param toChildPosition New child position of the item.
* @param result Indicates whether the dragging operation was succeeded.
*/
void onChildDragFinished(int fromGroupPosition, int fromChildPosition, int toGroupPosition, int toChildPosition, boolean result);
}
Loading

0 comments on commit 84d5818

Please sign in to comment.