In Recycler View, the delete-drawable gets cleared off only after a tap when the cancellation to the swipe.
I don't know what I'm doing wrong with code. I Am clearing the canvas with CLEAR mode, but it is visible after the cancellation of the swipe. Please find the GIF file for more details.
abstract public class SwipeToDeleteHelper extends ItemTouchHelper.Callback {
Context context;
private Paint paint;
private ColorDrawable Background;
private int BackgroundColour;
private Drawable DeleteDrawable;
private int intrinsicWidth;
private int intrinsicHeight;
public SwipeToDeleteHelper(Context context) {
this.context = context;
Background = new ColorDrawable();
BackgroundColour = Color.parseColor("#ff3d00");
DeleteDrawable = ContextCompat.getDrawable(context,R.drawable.ic_delete);
intrinsicHeight = DeleteDrawable.getIntrinsicHeight();
intrinsicWidth = DeleteDrawable.getIntrinsicHeight(); }
@Override
public int getMovementFlags(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(0,ItemTouchHelper.LEFT );
}
@Override
public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
return false;}
@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 view = viewHolder.itemView;
int itemHeight = view.getHeight();
Boolean isCancelled = dX==0 && isCurrentlyActive;
if(isCancelled)
{
clearCanvas(c,view.getRight()+dX,(float)view.getTop(),(float)view.getRight(),(float)view.getBottom());
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
return;}
Background.setColor(BackgroundColour);
Background.setBounds(view.getRight() + (int) dX, view.getTop(), view.getRight(), view.getBottom());
Background.draw(c);
int DeleteIconTop = view.getTop() + (itemHeight - intrinsicHeight) / 2;
int DeleteIconMargin = (itemHeight - intrinsicHeight) / 2;
int DeleteIconLeft = view.getRight() - DeleteIconMargin - intrinsicWidth;
int DeleteIconRight = view.getRight() - DeleteIconMargin;
int DeleteIconBottom = DeleteIconTop + intrinsicHeight;
DeleteDrawable.setBounds(DeleteIconLeft, DeleteIconTop, DeleteIconRight, DeleteIconBottom);
DeleteDrawable.draw(c);
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
private void clearCanvas(Canvas c, float left, float top, float right, float bottom) {
Paint clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
c.drawRect(left,top,right,bottom,clearPaint);
}
@Override
public float getSwipeThreshold(@NonNull RecyclerView.ViewHolder viewHolder) {
return 0.7f; //super.getSwipeThreshold(viewHolder);
}
}
this lines avoided me the error. c.clipRect(), c.save() and c.restore()