package com.sjkj.appthreefloor_tsgz.utils;


import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

import com.sjkj.appthreefloor_tsgz.R;


public class SlideItemTouchHelper extends ItemTouchHelper.Callback {

    private ItemSlideCallBack itemSlideCallBack;
    Context context;

    public SlideItemTouchHelper(Context context, ItemSlideCallBack mItemSlideCallBack) {
        itemSlideCallBack = mItemSlideCallBack;
        this.context = context;
    }

    /**
     * 设置滑动类型标记
     */

    @Override
    public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
        return makeMovementFlags(ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT, 0);
    }

    /**
     * 拖拽交换Item的回调
     */

    @Override
    public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder viewHolder1) {
        if (null != itemSlideCallBack) {
            itemSlideCallBack.onMove(viewHolder.getAdapterPosition(), viewHolder1.getAdapterPosition());
        }
        return true;
    }

    /**
     * 滑动Item
     */

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

    }

    @Override
    public void onSelectedChanged(@Nullable RecyclerView.ViewHolder viewHolder, int actionState) {
        super.onSelectedChanged(viewHolder, actionState);
        if (null != viewHolder) {
            viewHolder.itemView.setBackground(context.getResources().getDrawable(R.drawable.shape_border));
        }

    }

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