简体   繁体   English

使用SpannedGridLayoutManager的ItemTouchHelper-RecyclerView问题

[英]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. 我在RecyclerView中将ItemTouchHelper与SpannedGridLayoutManager结合使用时遇到问题,当拖动到拖动对象旁边的项目时,拖动会过早结束。 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 onSelectedChanged(RecyclerView.ViewHolder,int)回调提供有关当前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! 因此,您可以跟踪订单是否更改,以及当状态更改为ACTION_STATE_IDLE时,您可以执行所需的操作!

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;
    }
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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