在“回收者视图”中,只有在点击cancellation
时才轻敲可删除绘图。
我不知道我在用代码做错什么。我正在使用canvas
模式清除CLEAR
,但是取消取消后可以看到它。请找到GIF
文件以获取更多详细信息。
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);
}
}
答案 0 :(得分:0)
此行避免了我的错误。 c.clipRect(), c.save()和 c.restore()
DeleteSwipe.setBounds(myItem.getLeft(),myItem.getTop(),(int)dX,myItem.getBottom());
DeleteIcon = ContextCompat.getDrawable(getActivity(),R.drawable.ic_delete);
DeleteIcon.setBounds(IconLeft,IconTop,IconRight,IconBottom);
DeleteSwipe.draw(c);
c.save();
c.clipRect(myItem.getLeft(),myItem.getTop(),(int)dX,myItem.getBottom());
DeleteIcon.draw(c);