package cn.diaovision.omnicontrol.widget;

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

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

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

public class TermItemTouchCallback extends ItemTouchHelper.Callback {
    TermItemAdapter adapter;

    public TermItemTouchCallback(TermItemAdapter adapter) {
        this.adapter = adapter;
    }

    @Override
    public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
        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 false;
    }

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

    }

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

    @Override
    public void clearView(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
        super.clearView(recyclerView, viewHolder);
        if (onDragListener != null) {
            onDragListener.onFinishDrag(viewHolder);
        }
    }

    private OnDragListener onDragListener;

    public TermItemTouchCallback setOnDragListener(OnDragListener onDragListener) {
        this.onDragListener = onDragListener;
        return this;
    }

    public interface OnDragListener {
        void onFinishDrag(RecyclerView.ViewHolder viewHolder);
    }
}

@@ -145,74 +145,8 @@
 r;%0A%0A
-import cn.diaovision.omnicontrol.widget.adapter.TermItemAdapter;%0A%0A
 /**%0A
@@ -260,37 +260,51 @@
 k %7B%0A    
-TermItemAdapter adapt
+private OnDragListener onDragListen
 er;%0A%0A   
@@ -337,66 +337,11 @@
 ack(
-TermItemAdapter adapter) %7B%0A        this.adapter = adapter;
+) %7B
 %0A   
@@ -447,32 +447,51 @@
 r viewHolder) %7B%0A
+        //%E5%8F%AF%E4%BB%A5%E4%B8%8A%E4%B8%8B%E5%B7%A6%E5%8F%B3%E6%8B%96%E6%8B%BD%0A
         final in
@@ -594,16 +594,32 @@
 .RIGHT;%0A
+        //%E4%B8%8D%E9%9C%80%E8%A6%81%E6%BB%91%E5%8A%A8%0A
         
@@ -875,18 +875,16 @@
 ) %7B%0A
-/*
         
 view
@@ -883,614 +883,36 @@
     
-viewHolder.itemView.bringToFront();%0A        int fromPosition = viewHolder.getAdapterPosition();//%E5%BE%97%E5%88%B0%E6%8B%96%E5%8A%A8ViewHolder%E7%9A%84position%0A        int toPosition = target.getAdapterPosition();//%E5%BE%97%E5%88%B0%E7%9B%AE%E6%A0%87ViewHolder%E7%9A%84position%0A        if (fromPosition %3C toPosition) %7B%0A            for (int i = fromPosition; i %3C toPosition; i++) %7B%0A                Collections.swap(adapter.getList(), i, i + 1);%0A            %7D%0A        %7D else %7B%0A            for (int i = fromPosition; i %3E toPosition; i--) %7B%0A                Collections.swap(adapter.getList(), i, i - 1);%0A            %7D%0A        %7D%0A        adapter.notifyItemMoved(fromPosition, toPosition);*/
+//%E6%8B%96%E5%8A%A8%E8%BF%87%E7%A8%8B%E4%B8%AD%E4%B8%8D%E9%9C%80%E8%A6%81%E4%BA%A4%E6%8D%A2item%E4%BD%8D%E7%BD%AE%EF%BC%8C%E7%9B%B4%E6%8E%A5%E8%BF%94%E5%9B%9Efalse
 %0A   
@@ -1029,16 +1029,35 @@
 tion) %7B%0A
+        //%E4%B8%8D%E9%9C%80%E8%A6%81%E6%BB%91%E5%8A%A8%E5%88%A0%E9%99%A4%E6%95%88%E6%9E%9C
 %0A    %7D%0A%0A
@@ -1112,24 +1112,64 @@
 Enabled() %7B%0A
+        //%E9%9C%80%E8%A6%81%E6%A0%B9%E6%8D%AEitem%E7%9A%84%E4%BD%8D%E7%BD%AE%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E5%8F%AF%E4%BB%A5%E6%8B%96%E6%8B%BD%EF%BC%8C%E6%89%80%E4%BB%A5%E8%BF%94%E5%9B%9Efalse%0A
         retu
@@ -1333,32 +1333,69 @@
 w, viewHolder);%0A
+        //%E6%8B%96%E6%8B%BD%E7%BB%93%E6%9D%9F%E5%90%8E%EF%BC%8C%E5%B0%86%E5%AE%8C%E6%88%90%E6%8B%96%E6%8B%BD%E7%9A%84viewHolder%E5%9B%9E%E8%B0%83%E5%87%BA%E5%8E%BB%0A
         if (onDr
@@ -1494,47 +1494,21 @@
     
-private OnDragListener onDragListener;%0A
+/*%E8%AE%BE%E7%BD%AE%E6%8B%96%E6%8B%BD%E5%AE%8C%E6%88%90%E7%9A%84%E5%9B%9E%E8%B0%83*/
 %0A   
@@ -1658,24 +1658,42 @@
 his;%0A    %7D%0A%0A
+    /*%E6%8B%96%E6%8B%BD%E5%AE%8C%E6%88%90%E7%9A%84%E5%9B%9E%E8%B0%83%E6%8E%A5%E5%8F%A3*/%0A
     public i

delete the TermItemAdapter field