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

问题描述 投票:0回答:1

我有一个片段,其中包含带有项目的RecyclerView。我已经实现了“删除滑动”功能,该功能可以正常工作。但是,我的RecyclerView中的项目的背景带有圆角,而在滑动时显示在项目后面的背景(带有垃圾桶图标)没有圆角(请参见屏幕截图)。有人可以帮我在这里弄个圆角吗?重新搜索之后,我遇到了drawRoundRectCanvas方法,但是,对于ColorDrawable对象,我将需要这种方法。

这是OnChildDrawItemTouchHelper方法的代码,其中绘制了当前背景:

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

enter image description here

java android android-recyclerview swipe rounded-corners
1个回答
0
投票

我设法使用GradientDrawable而不是ColorDrawable来解决我的问题,该方法具有setCornerRadius()的方法。

© www.soinside.com 2019 - 2024. All rights reserved.