2

I was implementing swipe to show Delete button in Recycler View , which I was able to do with the help of this https://stackoverflow.com/questions/44965278/recyclerview-itemtouchhelper-buttons-on-swipe . But my requirement is to add an image along with the Delete option when swiped to right.

How can we the set the Image from the drawable to the onDraw Method. can someone help me on this. I have added an Image which I need to implement like the same.enter image description here

code::

 public static class UnderlayButton {
        private String text;
        private int imageResId;
        private int color;
        private int pos;
        private RectF clickRegion;
        private UnderlayButtonClickListener clickListener;

        public UnderlayButton(String text, int imageResId, int color, UnderlayButtonClickListener clickListener) {
            this.text = text;
            this.imageResId = imageResId;
            this.color = color;
            this.clickListener = clickListener;
        }

        public boolean onClick(float x, float y) {
            if (clickRegion != null && clickRegion.contains(x, y)) {
                clickListener.onClick(pos);
                return true;
            }

            return false;
        }

        public void onDraw(Canvas c, RectF rect, int pos) {
            Paint p = new Paint();

            // Draw background
            p.setColor(color);
            c.drawRect(rect, p);

            // Draw Text
            p.setColor(Color.WHITE);
            p.setTextSize(20);


            Rect r = new Rect();
            float cHeight = rect.height();
            float cWidth = rect.width();
            p.setTextAlign(Paint.Align.LEFT);
            p.getTextBounds(text, 0, text.length(), r);
            float x = cWidth / 2f - r.width() / 2f - r.left;
            float y = cHeight / 2f + r.height() / 2f - r.bottom;
            c.drawText(text, rect.left + x, rect.top + y, p);

            clickRegion = rect;
            this.pos = pos;
        }
    }
I'm Coder
  • 125
  • 2
  • 13
  • in onTouch() method, swipedViewHolder.itemView was null after deleting last item from adapter and list, any solution? How can we fix that issue in the above code? – I'm Coder Feb 16 '22 at 10:30

0 Answers0