Skip to content

Commit

Permalink
Merge pull request #1300 from FrankKwok/master
Browse files Browse the repository at this point in the history
fix #1299
  • Loading branch information
CymChad authored Jul 11, 2017
2 parents 0cc66b1 + b83a190 commit 583b37b
Showing 1 changed file with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ public void onBindViewHolder(K holder, int positions) {
}


/**
* Set the toggle view's id which will trigger drag event.
* If the toggle view id is not set, drag event will be triggered when the item is long pressed.
*
* @param toggleViewId the toggle view's id
*/
/**
* Set the toggle view's id which will trigger drag event.
* If the toggle view id is not set, drag event will be triggered when the item is long pressed.
*
* @param toggleViewId the toggle view's id
*/
public void setToggleViewId(int toggleViewId) {
mToggleViewId = toggleViewId;
}
Expand Down Expand Up @@ -198,16 +198,18 @@ public void onItemDragMoving(RecyclerView.ViewHolder source, RecyclerView.ViewHo
int from = getViewHolderPosition(source);
int to = getViewHolderPosition(target);

if (from < to) {
for (int i = from; i < to; i++) {
Collections.swap(mData, i, i + 1);
}
} else {
for (int i = from; i > to; i--) {
Collections.swap(mData, i, i - 1);
if (inRange(from) && inRange(to)) {
if (from < to) {
for (int i = from; i < to; i++) {
Collections.swap(mData, i, i + 1);
}
} else {
for (int i = from; i > to; i--) {
Collections.swap(mData, i, i - 1);
}
}
notifyItemMoved(source.getAdapterPosition(), target.getAdapterPosition());
}
notifyItemMoved(source.getAdapterPosition(), target.getAdapterPosition());

if (mOnItemDragListener != null && itemDragEnabled) {
mOnItemDragListener.onItemDragMoving(source, from, target, to);
Expand Down Expand Up @@ -243,8 +245,10 @@ public void onItemSwiped(RecyclerView.ViewHolder viewHolder) {

int pos = getViewHolderPosition(viewHolder);

mData.remove(pos);
notifyItemRemoved(viewHolder.getAdapterPosition());
if (inRange(pos)) {
mData.remove(pos);
notifyItemRemoved(viewHolder.getAdapterPosition());
}
}

public void onItemSwiping(Canvas canvas, RecyclerView.ViewHolder viewHolder, float dX, float dY, boolean isCurrentlyActive) {
Expand All @@ -253,4 +257,7 @@ public void onItemSwiping(Canvas canvas, RecyclerView.ViewHolder viewHolder, flo
}
}

private boolean inRange(int position) {
return position >= 0 && position < mData.size();
}
}

0 comments on commit 583b37b

Please sign in to comment.