RecyclerView实现item拖拽效果

使用android.support.v7.widget.helper 包下的ItemTouchHelper工具类
来处理RecyclerView的拖拽

效果如下:

效果图.gif

1.创建ItemTouchHelper类
2.调用mItemTouchHelper.attachToRecyclerView(recyclerview)关联recyclerview

recyclerview.setLayoutManager(gridLayoutManager);
adapter = new DragAdapter(this,datas);
recyclerview.setAdapter(adapter);
mItemTouchHelper = new ItemTouchHelper(new DragItemHelperCallBack());
mItemTouchHelper.attachToRecyclerView(recyclerview);

ItemTouchHelper的构造需要传入一个CallBack回调

public class DragItemHelperCallBack extends ItemTouchHelper.Callback {
    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        int dragFlags = 0;
        if(recyclerView.getLayoutManager() instanceof GridLayoutManager){
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN |
                    ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        }else {
            dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
        }
        // 支持左右滑动(删除)操作, swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END
        final int swipeFlags = 0;
        return makeMovementFlags(dragFlags,swipeFlags);
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        //被按下拖拽时候的position
        int fromPosition = viewHolder.getAdapterPosition();
        //当前拖拽到的item的posiiton
        int toPosition = target.getAdapterPosition();

        //回调到adapter 当中处理移动过程中,数据变更的逻辑,以及更新UI
        if (recyclerView.getAdapter() instanceof IOnItemMoveListener) {
            IOnItemMoveListener listener = ((IOnItemMoveListener) recyclerView.getAdapter());
            listener.onItemMove(fromPosition, toPosition);
        }

        return true;
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

    }

    @Override
    public void onSelectedChanged(RecyclerView.ViewHolder viewHolder, int actionState) {
        // 不在闲置状态
        if (actionState != ItemTouchHelper.ACTION_STATE_IDLE) {
            if (viewHolder instanceof IOnDragVHListener) {
                IOnDragVHListener itemViewHolder = (IOnDragVHListener) viewHolder;
                itemViewHolder.onItemSelected();
            }
        }
        super.onSelectedChanged(viewHolder, actionState);
    }

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if(viewHolder instanceof IOnDragVHListener){
            IOnDragVHListener itemViewHolder = (IOnDragVHListener) viewHolder;
            itemViewHolder.onItemFinish();
        }
        super.clearView(recyclerView, viewHolder);
    }
}

拖拽中会不断调用onMove方法,通过viewHolder和target获取到被按下拖拽时候的position和当前拖拽到的item的posiiton,然后接口回调 listener.onItemMove到recyclerView的Adapter当中处理数据变化的逻辑

Adapter当中的处理

 @Override
    public void onItemMove(int fromPosition, int toPosition) {
        /**
         * 拖拽过程中的数据处理
         * 1.先记录原来位置的数据
         * 2.删除原来位置的数据
         * 3.在拖拽到的位置插入原来的数据
         */
        String fromString = dataList.get(fromPosition);
        dataList.remove(fromPosition);
        dataList.add(toPosition,fromString);
        //通知数据发生移动
        notifyItemMoved(fromPosition, toPosition);
    }

onSelectedChanged方法和clearView方法是当发生拖拽的时候和拖拽完成的时候调用,可以在这里给我们对应的布局设置不同的颜色显示

//拖拽过程中调用
@Override
public void onItemSelected() {
      tv_content.setBackgroundResource(R.color.textColorPrimary);
}
//拖拽完成调用
@Override
public void onItemFinish() {
      tv_content.setBackgroundResource(R.color.viewBackground);
}

你可能感兴趣的:(RecyclerView实现item拖拽效果)