删除带有圆角背景的滑动项目

Delete item on swipe with rounded corners background

我有一个包含 RecyclerView 项目的片段。我已经实现了一个 "swipe to delete" 函数,它按预期工作。但是,我的 RecyclerView 中的项目具有带圆角的背景,而滑动时项目后面显示的背景(带有垃圾桶图标)没有圆角(参见屏幕截图)。任何人都可以帮我在那里得到圆角吗? 经过一些研究,我遇到了 CanvasdrawRoundRect 方法,但是,我需要 ColorDrawable 对象的这种方法。

这里是ItemTouchHelperOnChildDraw方法的代码,绘制当前背景的地方:

public class SwipeToDeleteCallback extends ItemTouchHelper.SimpleCallback {

    private RecyclerView.Adapter mAdapter;
    private Drawable icon;
    private final ColorDrawable background;
    private Context mContext;
    private Activity mActivity;
    private Fragment mFragment;


    @Override
    public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

        View itemView = viewHolder.itemView;
        int backgroundCornerOffset = 20;

        int iconMargin = (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
        int iconTop = itemView.getTop() + (itemView.getHeight() - icon.getIntrinsicHeight()) / 2;
        int iconBottom = iconTop + icon.getIntrinsicHeight();

        if (dX < 0) { // Swiping to the left
            int iconLeft = itemView.getRight() - iconMargin - icon.getIntrinsicWidth();
            int iconRight = itemView.getRight() - iconMargin;
            icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);


            background.setBounds(itemView.getRight() + ((int) dX) - backgroundCornerOffset,
                    itemView.getTop(), itemView.getRight(), itemView.getBottom());


        } else { // view is unSwiped
            background.setBounds(0, 0, 0, 0);
        }
        background.draw(c);
        icon.draw(c);
    }
}

以及我使用 drawRoundRect 方法尝试过的一些代码,但没有解决我的问题:

RectF bg = new RectF(itemView.getRight() + ((int) dX) - backgroundCornerOffset,
                    itemView.getTop(), itemView.getRight(), itemView.getBottom());
            Paint p = new Paint();
            p.setColor(Color.parseColor("#0000FF"));
            c.drawRoundRect(bg, 20, 20, p);

我设法使用 GradientDrawable 而不是 ColorDrawable 解决了我的问题,后者的方法是 setCornerRadius().