I have a recyclerView that supports drag and drop. When I drag a viewHolder to the end of the list it keeps dragging past the last index. What is the proper way to make it stop dragging when it reaches the end? Right now, when "dropped" it snaps back to the last position in the RecyclerView.
This is with recyclerView's height="match_parent"
This is with recyclerView's height="wrap_content"
After reading this post, I tried preventing this behavior in the onChildDraw method of the ItemTouchHelper. The 3 things I tried to stop the dragging at the last index all work, but the swapping animations aren't fluid at all. What is the proper way to handle this? Also, is this ItemTouchHelper/recyclerView's standard behavior (to not stop dragging at beginning/end of the list)? I find it hard to believe that the default behavior allows this and I'm wondering if it's my implementation that's causing this.
@Override
public void onChildDraw(@NonNull Canvas canvas,
@NonNull RecyclerView recyclerView,
@NonNull RecyclerView.ViewHolder viewHolder,
float dX, float dY, int actionState, boolean isCurrentlyActive) {
if(actionState == ItemTouchHelper.ACTION_STATE_DRAG) {
// FIRST THING I TRIED
// This works but slows the swapping animations down. When fast dragging an item down to the
// bottom it's like it hit a brick wall and the viewHolder bounces upward a little before settling in
// the last position
if(viewHolder.getAdapterPosition() == recyclerLastIndex || viewHolder.getAdapterPosition() == 0) {
return;
}
// SECOND THING I TRIED
// This also works but I lose the fluid swapping animations while dragging. The swap is very abrupt.
recyclerView.getLocationOnScreen(posXY); // method uses a 2 element array (posXY) to store X and Y coordinates
int yPosOfRecycler = posXY[1]; // Y coordinate is stored in the second position
viewHolder.itemView.getLocationOnScreen(posXY);
int yPosOfViewHolder = posXY[1];
if(yPosOfViewHolder > yPosOfRecycler) {
return;
}
// THIRD THING I TRIED
// Works but swapping animations are also lost
if(!recyclerView.canScrollVertically(1)) {
return;
}
// If I comment out the code in the 3 approaches above, the swapping animations are beautifully
// smooth, but will allow dragging past the boundaries of the recyclerView
super.onChildDraw(canvas, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}