简体   繁体   English

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

[英]Delete item on swipe with rounded corners background

I have a fragment that contains a RecyclerView with items.我有一个包含带有项目的RecyclerView的片段。 I have already implemented a "swipe to delete" function, which works as expected.我已经实现了“滑动删除”功能,它按预期工作。 However, the items in my RecyclerView have a background with rounded corners, whereas the background that is shown behind the item on swipe (with the trash icon) does not have rounded corners (see screenshot).但是,我的RecyclerView的项目具有带圆角的背景,而滑动时显示在项目后面的背景(带有垃圾桶图标)没有圆角(参见屏幕截图)。 Can anybody help me to get rounded corners there as well?有人可以帮我在那里得到圆角吗? After some reseaerching, I came across the drawRoundRect method for a Canvas , however, I would need such a method for a ColorDrawable object.经过一些研究,我遇到了CanvasdrawRoundRect方法,但是,我需要一个ColorDrawable对象的方法。

Here's the code of the OnChildDraw method of the ItemTouchHelper , where the current background is drawn:下面是OnChildDraw方法的ItemTouchHelper ,这里绘制了当前背景:

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);
    }
}

and some code of what I have tried using the drawRoundRect method, which did not resolve my issue:以及我使用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()方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM