package cn.diaovision.omnicontrol.widget;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;

import java.util.Collections;

import cn.diaovision.omnicontrol.widget.adapter.AuxiliaryPanelItemAdapter;

/**
 * Created by TaoYimin on 2017/5/5.
 */

public class MyItemTouchCallback extends ItemTouchHelper.Callback {
    AuxiliaryPanelItemAdapter adapter;

    public MyItemTouchCallback(AuxiliaryPanelItemAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) {
        final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        final int swipeFlags = 0;
        return makeMovementFlags(dragFlags, swipeFlags);
        } else {
            final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
            final int swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        }
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                          RecyclerView.ViewHolder target) {
        viewHolder.itemView.bringToFront();
        int fromPosition = viewHolder.getAdapterPosition();//得到拖动ViewHolder的position
        int toPosition = target.getAdapterPosition();//得到目标ViewHolder的position
        if (fromPosition < toPosition) {
            for (int i = fromPosition; i < toPosition; i++) {
                Collections.swap(adapter.getList(), i, i + 1);
            }
        } else {
            for (int i = fromPosition; i > toPosition; i--) {
                Collections.swap(adapter.getList(), i, i - 1);
            }
        }
        adapter.notifyItemMoved(fromPosition, toPosition);
        return true;
    }

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

    }

    @Override
    public boolean isLongPressDragEnabled() {
        return false;
    }
}

@@ -40,60 +40,8 @@
 t;%0A%0A
-import android.support.v7.widget.GridLayoutManager;%0A
 impo
@@ -83,16 +83,16 @@
 erView;%0A
+
 import a
@@ -145,39 +145,8 @@
 r;%0A%0A
-import java.util.Collections;%0A%0A
 impo
@@ -592,84 +592,8 @@
 ) %7B%0A
-        if (recyclerView.getLayoutManager() instanceof GridLayoutManager) %7B%0A
     
@@ -803,211 +803,8 @@
 s);%0A
-        %7D else %7B%0A            final int dragFlags = ItemTouchHelper.UP %7C ItemTouchHelper.DOWN;%0A            final int swipeFlags = 0;%0A            return makeMovementFlags(dragFlags, swipeFlags);%0A        %7D%0A
     
@@ -965,24 +965,26 @@
 r target) %7B%0A
+/*
         view
@@ -1015,16 +1015,16 @@
 ront();%0A
-
         
@@ -1583,16 +1583,18 @@
 sition);
+*/
 %0A       
@@ -1770,24 +1770,24 @@
 Enabled() %7B%0A
-
         retu
@@ -1801,11 +1801,539 @@
 e;%0A    %7D
+%0A%0A    @Override%0A    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) %7B%0A        super.clearView(recyclerView, viewHolder);%0A        if (onDragListener != null) %7B%0A            onDragListener.onFinishDrag();%0A        %7D%0A    %7D%0A%0A    private OnDragListener onDragListener;%0A%0A    public MyItemTouchCallback setOnDragListener(OnDragListener onDragListener) %7B%0A        this.onDragListener = onDragListener;%0A        return this;%0A    %7D%0A%0A    public interface OnDragListener %7B%0A        void onFinishDrag();%0A    %7D
 %0A%7D%0A

删除了辅助屏RecyclerView的拖拽排序功能，添加了拖拽完成的监听