简体   繁体   中英

Hover event in RecyclerView using ItemTouchHelper

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.

在此处输入图片说明

Any ideas how I can achieve this? Thanks!

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

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!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM