Skip to content

Instantly share code, notes, and snippets.

@keinix
Created September 12, 2018 11:48
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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);
}
}
@araxis
Copy link

araxis commented Jan 18, 2019

hello.
first sorry for my bad english
i use your code in my project it is great, but i had to apply this change
this-> if (dX > 0) { // Swiping to the right
int iconLeft = itemView.getLeft() + iconMargin + icon.getIntrinsicWidth();
int iconRight = itemView.getLeft() + iconMargin;

to->
if (dX > 0) { // Swiping to the right
int iconLeft = itemView.getLeft() + iconMargin;
int iconRight = iconLeft+ icon.getIntrinsicWidth();

@millesm
Copy link

millesm commented Mar 21, 2019

is there a full working project for this as I've been unable to retrofit into my app (simply not experienced enough at java and android).

what are the last two import statements?

just loading this code into my sample app I get errors with 'getcontext()' in the following icon = ContextCompat.getDrawable(mAdapter.getContext(),
R.drawable.ic_delete_white_36);

and the deletetask() function cannot be resolved.

@sproctor
Copy link

sproctor commented Apr 7, 2019

@millesm:

You can create the asset for R.drawable.ic_delete_white_36. File -> New -> Vector Asset. You can pick your icon from there.

Then create the class MyAdapter with a method named deleteTask then pass an instance of that to the constructor of SwipeToDeleteCallback.

@NanyangTaiji
Copy link

Where I can find a complete project zip for this?

@ZaccharieBOUVY
Copy link

@araxis +1 ! thank you for sharing

@timobarrett
Copy link

Code leaves the bloody trash can in the middle of the row if the user doesn't complete the swipe!

@aabhasr1
Copy link

Hi,
there is a bug on this for left to right swipe

line 55 and 56 should be

int iconLeft = itemView.getLeft() + iconMargin;
int iconRight = itemView.getLeft() + iconMargin + icon.getIntrinsicWidth();

@AlienAsRoger
Copy link

Hi,
there is a bug on this for left to right swipe

line 55 and 56 should be

int iconLeft = itemView.getLeft() + iconMargin;
int iconRight = itemView.getLeft() + iconMargin + icon.getIntrinsicWidth();

Thank you! That saved my day :)

@kovacsakos
Copy link

kovacsakos commented Dec 5, 2019

My problem was that the icon not disappeared after the user cancelled the swipe.
This solved my problem:

} else {  // view is unSwiped
      icon.setBounds(0, 0, 0, 0);     // ADD THIS LINE
      background.setBounds(0, 0, 0, 0);
   }

@russelg1
Copy link

Can you provide a link to the TaskAdapter code and coordinator_layout.xml. Thanks

@hereisderek
Copy link

My problem was that the icon not disappeared after the user cancelled the swipe.
This solved my problem:

} else {  // view is unSwiped
      icon.setBounds(0, 0, 0, 0);     // ADD THIS LINE
      background.setBounds(0, 0, 0, 0);
   }

or you just don't draw it

@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