FastAdapter icon indicating copy to clipboard operation
FastAdapter copied to clipboard

Drop callback

Open hsdmiranda opened this issue 10 years ago • 8 comments

Hi,

I have a user case that I need to do some actions after a drag and drop. The problem with the current implementation is I don't know when the item is really dropped. It is very expensive to execute the call every time the item move over the list. Any suggestion how should I implement that?

Another issue I have with drag and drop is that I don't want all my items in the recyclerView to be draggable.

hsdmiranda avatar Feb 27 '16 09:02 hsdmiranda

@hsdmiranda

regarding your first topic. You can extend the SimpleDragCallback and overwrite the

@Override
    public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
        super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
    }

For the second thing I've implemented a IDraggable interface which will be checked in the SimpleDragCallback and disable the drag feature for the items which are not draggable.

This is in v1.2.2

mikepenz avatar Mar 01 '16 19:03 mikepenz

In case someone needs to know when an item has been dropped, this is how I implemented:

I extended the original interface from fastAdapterExtensions and added one more callback method. public interface ImprovedItemTouchCallback extends ItemTouchCallback { void onDrop(Integer mFrom, Integer mTo); }

And this is my callback that remembers the location of the item to send the information on the onDrop. I also added a small snippet that checks if the target is draggable, so my item does not swipe with it (very specific for my user case i guess, it can be removed)

public class ImprovedDragCallback extends SimpleDragCallback {

    private Integer mFrom = null;
    private Integer mTo = null;
    private ImprovedItemTouchCallback callback;

    public ImprovedDragCallback(ImprovedItemTouchCallback itemTouchCallback) {
        super(itemTouchCallback);
        this.callback = itemTouchCallback;
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        if (target.itemView.getTag() instanceof IDraggable) {
            if (((IDraggable) target.itemView.getTag()).isDraggable()) {
                if (mFrom == null) {
                    mFrom = viewHolder.getAdapterPosition();
                }
                mTo = target.getAdapterPosition();

                return super.onMove(recyclerView, viewHolder, target);
            }
        }

        return false;
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);

        if (mFrom != null && mTo != null) {
            callback.onDrop(mFrom, mTo);
        }

        // clear saved positions
        mFrom = mTo = null;
    }
}

hsdmiranda avatar Mar 04 '16 14:03 hsdmiranda

Maybe you would like to try a pr?

Rainer-Lang avatar Mar 04 '16 14:03 Rainer-Lang

I tried to make the project work on my machine and was failing for some weird reasons. So, I gave up on the pull request. I will give another try later, with more appropriated naming.

hsdmiranda avatar Mar 04 '16 14:03 hsdmiranda

Please let us know if we could help you.

Rainer-Lang avatar Mar 04 '16 14:03 Rainer-Lang

@hsdmiranda so the onMoved() method does not fire after dropping an item?

mikepenz avatar Mar 04 '16 21:03 mikepenz

Sorry for the delay, crazy week here. @mikepenz the onMoved its called before the oDrop. The onMove is called every time an item passes over another item.

hsdmiranda avatar Mar 07 '16 08:03 hsdmiranda

@hsdmiranda no problem. Oh cool. :)

mikepenz avatar Mar 07 '16 11:03 mikepenz