-
-
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); | |
} | |
} |
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.
Where I can find a complete project zip for this?
@araxis +1 ! thank you for sharing
Code leaves the bloody trash can in the middle of the row if the user doesn't complete the swipe!
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();
Hi,
there is a bug on this for left to right swipeline 55 and 56 should be
int iconLeft = itemView.getLeft() + iconMargin;
int iconRight = itemView.getLeft() + iconMargin + icon.getIntrinsicWidth();
Thank you! That saved my day :)
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);
}
Can you provide a link to the TaskAdapter code and coordinator_layout.xml. Thanks
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
for me, Icon does not show. icon.draw(c)
is not working
@ZaccharieBOUVY you're welcome.
thanks man!
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.