Skip to content

Instantly share code, notes, and snippets.

@keinix
Created September 12, 2018 11:48
Show Gist options
  • Select an option

  • Save keinix/b1aa2417dbea9311a1207eddf8b9d47b to your computer and use it in GitHub Desktop.

Select an option

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);
}
}
@millesm

millesm commented Mar 21, 2019

Copy link
Copy Markdown

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

sproctor commented Apr 7, 2019

Copy link
Copy Markdown

@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
Copy Markdown

Where I can find a complete project zip for this?

@ZaccharieBOUVY

Copy link
Copy Markdown

@araxis +1 ! thank you for sharing

@timobarrett

Copy link
Copy Markdown

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

@aabhasr1

Copy link
Copy Markdown

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
Copy Markdown

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

kovacsakos commented Dec 5, 2019

Copy link
Copy Markdown

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
Copy Markdown

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

@hereisderek

Copy link
Copy Markdown

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
Copy Markdown

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

@araxis

araxis commented Dec 22, 2021

Copy link
Copy Markdown

@ZaccharieBOUVY you're welcome.

@e-tverdokhleb

Copy link
Copy Markdown

thanks man!

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