简体   繁体   中英

ItemTouchHelper with SpannedGridLayoutManager - RecyclerView issues

I am facing an issue with ItemTouchHelper in combination with SpannedGridLayoutManager in my RecyclerView, drag is ended prematurely when dragging to the item next to the dragged one. I know it is glitchy layout manager, because it works with other layout managers without any issues.

Did somebody worked this out already?

The onSelectedChanged(RecyclerView.ViewHolder, int) callback provides information about the current actionState: - ACTION_STATE_IDLE: - ACTION_STATE_DRAG - ACTION_STATE_SWIPE

So you could keep track whether the order changed, and when the state changes to ACTION_STATE_IDLE, you can do what you need to do!

Implement a callback class like this.

class CardsTouchHelperCallback extends ItemTouchHelper.Callback {

...
@Override
    public boolean onMove(RecyclerView recyclerView, 
RecyclerView.ViewHolder viewHolder,
                      RecyclerView.ViewHolder target) {

    int fromPosition = viewHolder.getAdapterPosition();
    int toPosition = target.getAdapterPosition();

    dragFrom =  fromPosition;
    dragTo = toPosition;

    mOrderChanged = true;

    return false;
}

@Override
public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
    super.onSelectedChanged(viewHolder, actionState);

    if (actionState == ItemTouchHelper.ACTION_STATE_IDLE && mOrderChanged) {
        //doSomething();
        touchHelperAdapter.onItemMove(dragFrom, dragTo);
        mOrderChanged = false;
    }
 }
}

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