package com.sound.prankparty.Utils;

import android.graphics.Canvas;
import android.view.View;

import androidx.recyclerview.widget.ItemTouchHelper;
import androidx.recyclerview.widget.RecyclerView;

public class MyItemTouchHelperCallback extends ItemTouchHelper.SimpleCallback {

    private float maxDistance; // 最大滑动距离
    private boolean isMaxSwiped = false; // 是否达到最大距离
    private RecyclerView.ViewHolder swipedViewHolder; // 被滑动的ViewHolder

    public MyItemTouchHelperCallback() {
        super(0, ItemTouchHelper.LEFT); // 只允许左滑
    }

    @Override
    public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
        return false; // 不处理移动事件
    }

    @Override
    public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
        // 当滑动项被完全滑出时，可以在这里处理
    }

    @Override
    public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder,
                            float dX, float dY, int actionState, boolean isCurrentlyActive) {
        // 限制最大滑动距离
        maxDistance = recyclerView.getWidth() * 0.5f; // 最大滑动为RecyclerView宽度的50%
        if (dX < -maxDistance) {
            dX = -maxDistance;
            isMaxSwiped = true;
            swipedViewHolder = viewHolder;
        } else {
            isMaxSwiped = false;
        }
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
    }

    @Override
    public float getSwipeThreshold(RecyclerView.ViewHolder viewHolder) {
        // 阻止自动滑出，始终返回1.0f
        return 1.0f;
    }

    // 复原滑动项的方法
    public void restoreSwipedItem() {
        if (swipedViewHolder != null) {
            View itemView = swipedViewHolder.itemView;
            itemView.animate().translationX(0).setDuration(200).start();
            swipedViewHolder = null;
        }
    }


}


