1

I have a RecyclerView and implemented drag and drop functionality using the ItemTouchHelper and ItemTouchHelper.Callback.

In ItemTouchHelper.Callback I have the callback method onMove() which gets called after the dragged view was dragged over the target view and the swap is completed.

What I am trying to implement (or find) is a new callback that is called when the dragged view is over the target view, before the swap is made. As you can see in the GIF below.

enter image description here

Any ideas how I can achieve this? Thanks!

  • LayoutManager takes care of the view positions. Dig deeper into LinearLayoutManager in association with ItemAnimator... There is a great video from the guys who created RecyclerView https://www.youtube.com/watch?v=LqBlYJTfLP4 – Samuel Robert Dec 20 '17 at 15:50
  • Have you looked this [example](https://github.com/iPaulPro/Android-ItemTouchHelper-Demo) ? – elgringo Dec 20 '17 at 15:50
  • @elgringo Yes, that is the tutorial that I followed for the drag and drop implementation. – Marius Andrei Rosu Dec 20 '17 at 15:53
  • Can I know you purpose of Getting that thing before swap – Abubakker Moallim Dec 20 '17 at 15:54
  • @ABDevelopers I want to drag and drop items to create a folder. Before I move the item, I need to show a preview of what will happen before the move, and the preview should be displayed while the dragged view is over the target view, but before it is moved. – Marius Andrei Rosu Dec 20 '17 at 15:59
  • OK understood its like you are dragging and putting something in folder and Want to give some sort of popup message before it actually puts @MariusAndreiRosu – Abubakker Moallim Dec 20 '17 at 16:01
  • @ABDevelopers Exactly! And I can't find a way to trigger an event when that happens. It must have a solution since a lot of apps, like when you create a folder on the home screen, do this. – Marius Andrei Rosu Dec 20 '17 at 16:03

2 Answers2

1

I found a solution for the question that I asked so I'm gonna post it here.

ItemTouchHelper.Callback has a method called choseDropTarget() which selects a drop target from the list of ViewHolders that are under the dragged view and it is called multiple times while dragging the view. Here we have information to calculate when the dragged view is hovering the view below and when the hovering stops.

@Override
public RecyclerView.ViewHolder chooseDropTarget(RecyclerView.ViewHolder selected, List<RecyclerView.ViewHolder> dropTargets, int curX, int curY) {
    for (RecyclerView.ViewHolder target : dropTargets) {
            int top = curY - selected.itemView.getTop();
            int bottom = curY + selected.itemView.getHeight();
            int diff;
            if (top < 0) {
                diff = target.itemView.getTop() - curY;
            } else {
                diff = target.itemView.getBottom() - bottom;
            }
            if (Math.abs(diff) <= 100) {
                adapter.onItemHover();
            } else {
                adapter.onItemHoverFinish();
            }
    }
    return super.chooseDropTarget(selected, dropTargets, curX, curY);
}

The method calculates the offset difference between the dragged view and the bottom view and if the difference is less than 100 onItemHover() callback is called and onItemHoverFinished() otherwise.

Let me know if you have a more elegant approach for this issue. Thanks!

0

Here before Swap you can put your Dialog or condition and on success you can swap the positions.

@Override
public boolean onItemMove(int fromPosition, int toPosition) {
    if (fromPosition < toPosition) {
        for (int i = fromPosition; i < toPosition; i++) {
            Collections.swap(mItems, i, i + 1);
        }
    } else {
        for (int i = fromPosition; i > toPosition; i--) {
            Collections.swap(mItems, i, i - 1);
        }
    }
    notifyItemMoved(fromPosition, toPosition);
    return true;
}

I havnt tested yet but hopefully this will help

Abubakker Moallim
  • 1,610
  • 10
  • 20
  • Unfortunately `onItemMove` is called only after the dragged item is dragged beyond the item below. I need to show the preview when the dragged item is exactly on top of the item below, like in the GIF that I attached. Same goes for `onMove` and `onMoved` callback methods from `ItemTouchHelper.Callback`. – Marius Andrei Rosu Dec 20 '17 at 16:14
  • But while it is dragged on another item. I think it will work. Because even while putting in folder you dont need to make item exactly over that folder. Just a bit hover on it is fine to put that thing inside – Abubakker Moallim Dec 20 '17 at 16:17