删除带有圆角背景的滑动项 [英] Delete item on swipe with rounded corners background

查看:74
本文介绍了删除带有圆角背景的滑动项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个片段,其中包含一个带有项目的 RecyclerView.我已经实现了滑动删除"功能,它按预期工作.但是,我的 RecyclerView 中的项目具有带圆角的背景,而滑动时显示在项目后面的背景(带有垃圾桶图标)没有圆角(参见屏幕截图).有人可以帮我在那里得到圆角吗?经过一番研究,我发现了 CanvasdrawRoundRect 方法,但是,对于 ColorDrawable 对象,我需要这样的方法.>

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

public class SwipeToDeleteCallback extends ItemTouchHelper.SimpleCallback {私有 RecyclerView.Adapter mAdapter;私人可绘制图标;私人最终 ColorDrawable 背景;私有上下文 mContext;私人活动 mActivity;私有片段 mFragment;@覆盖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);查看 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) {//向左滑动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 {//视图未滑动background.setBounds(0, 0, 0, 0);}背景绘制(c);icon.draw(c);}}

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

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

解决方案

我设法使用 GradientDrawable 而不是 ColorDrawable 解决了我的问题,它具有方法 <代码>setCornerRadius().

I have a fragment that contains a RecyclerView with items. 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). Can anybody help me to get rounded corners there as well? After some reseaerching, I came across the drawRoundRectmethod for a Canvas, however, I would need such a method for a ColorDrawableobject.

Here's the code of the OnChildDraw method of the ItemTouchHelper, where the current background is drawn:

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:

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

解决方案

I managed to solve my problem using a GradientDrawable instead of a ColorDrawable, which has the method setCornerRadius().

这篇关于删除带有圆角背景的滑动项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆