Skip to content

Instantly share code, notes, and snippets.

@keinix
Created September 12, 2018 11:48
Show Gist options
  • Save keinix/b1aa2417dbea9311a1207eddf8b9d47b to your computer and use it in GitHub Desktop.
Save keinix/b1aa2417dbea9311a1207eddf8b9d47b to your computer and use it in GitHub Desktop.
package io.keinix.protoflow.util;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
import io.keinix.protoflow.R;
import io.keinix.protoflow.tasks.TasksAdapter;
public class SwipeToDeleteCallback extends ItemTouchHelper.SimpleCallback {
private TasksAdapter mAdapter;
private Drawable icon;
private final ColorDrawable background;
public SwipeToDeleteCallback(MyAdapter adapter) {
super(0,ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT);
mAdapter = adapter;
icon = ContextCompat.getDrawable(mAdapter.getContext(),
R.drawable.ic_delete_white_36);
background = new ColorDrawable(Color.RED);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
// used for up and down movements
return false;
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
int position = viewHolder.getAdapterPosition();
mAdapter.deleteTask(position);
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, 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; //so background is behind the rounded corners of itemView
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 right
int iconLeft = itemView.getLeft() + iconMargin + icon.getIntrinsicWidth();
int iconRight = itemView.getLeft() + iconMargin;
icon.setBounds(iconLeft, iconTop, iconRight, iconBottom);
background.setBounds(itemView.getLeft(), itemView.getTop(),
itemView.getLeft() + ((int) dX) + backgroundCornerOffset, itemView.getBottom());
} else 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);
}
}
@lekeCoder
Copy link

for me, Icon does not show. icon.draw(c) is not working

@araxis
Copy link

araxis commented Dec 22, 2021

@ZaccharieBOUVY you're welcome.

@e-tverdokhleb
Copy link

thanks man!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment