Drop callback
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
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
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;
}
}
Maybe you would like to try a pr?
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.
Please let us know if we could help you.
@hsdmiranda so the onMoved() method does not fire after dropping an item?
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 no problem. Oh cool. :)