首页 > 解决方案 > Swipe delete on Recycleview is very sensitive

问题描述

I have a Recycleview with CardView and I Implement to this a swipe handle option. When the user swipe right the card need to delete. The problem is that the action is very sensitive - When you click or drag a little bit the card it do the action. What can I do to fix that to be less sensitive, only if you drag the card until the end of the screen for example?

标签: androidandroid-studio

解决方案


Just override method getSwipeEscapeVelocity() in class ItemTouchHelper.Callback:

public class SwipeToDeleteTouchHelperCallback extends ItemTouchHelper.SimpleCallback {
    //constructor, another methods, etc...

    @Override
    public float getSwipeEscapeVelocity(float defaultValue) {
        return defaultValue * 10;//10 -> almost insensitive
    }
}

If you want change "borderline of swipe", override another method in this class:

@Override
public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
    // 0.75 - you need to drag item by 75% of his width(or height) to dismiss
    // default value is 0.5f
    return 0.75f;
}

推荐阅读