Permalink
Browse files

Add swipe draw

  • Loading branch information...
david-szabo97 committed Dec 11, 2017
1 parent 5394bb5 commit 43a45d1cd6921378d518251ffdb538caf0ffe3e5
@@ -1,10 +1,37 @@
package com.messedcode.swipecollapseanimationexample;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.View;
public class CustomItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final OnSwipeListener onSwipeListener;
private final int leftBackgroundColor;
private final Bitmap leftIcon;
private final int rightBackgroundColor;
private final Bitmap rightIcon;
private final float iconSize;
private final Paint paint = new Paint();
private CustomItemTouchHelperCallback(Builder b) {
this.onSwipeListener = b.onSwipeListener;
this.leftBackgroundColor = b.leftBackgroundColor;
this.leftIcon = b.leftIcon;
this.rightBackgroundColor = b.rightBackgroundColor;
this.rightIcon = b.rightIcon;
this.iconSize = b.iconSize;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
return makeMovementFlags(0, ItemTouchHelper.START | ItemTouchHelper.END);
@@ -17,6 +44,108 @@ public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHol
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
if (onSwipeListener != null) {
if (direction == ItemTouchHelper.START) {
onSwipeListener.onSwipeLeft(viewHolder);
} else if (direction == ItemTouchHelper.END) {
onSwipeListener.onSwipeRight(viewHolder);
}
}
}
@Override
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {
if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
View itemView = viewHolder.itemView;
float width = itemView.getWidth();
float height = itemView.getHeight();
if (height > 0) {
float left = itemView.getLeft();
float top = itemView.getTop();
float right = itemView.getRight();
float bottom = itemView.getBottom();
float centerY = (top + bottom) / 2;
float margin = width * 0.025f;
if (dX > 0) {
paint.setColor(leftBackgroundColor);
RectF background = new RectF(left, top, dX, bottom);
c.drawRect(background, paint);
float iconLeft = left + margin;
RectF iconRect = new RectF(iconLeft, centerY - iconSize / 2, iconLeft + iconSize, centerY + iconSize / 2);
c.drawBitmap(leftIcon, null, iconRect, paint);
} else {
paint.setColor(rightBackgroundColor);
RectF background = new RectF(right + dX, top, right, bottom);
c.drawRect(background, paint);
float iconRight = right - margin;
RectF iconRect = new RectF(iconRight - iconSize, centerY - iconSize / 2, iconRight, centerY + iconSize / 2);
c.drawBitmap(rightIcon, null, iconRect, paint);
}
}
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
public interface OnSwipeListener {
void onSwipeRight(RecyclerView.ViewHolder vh);
void onSwipeLeft(RecyclerView.ViewHolder vh);
}
public static class Builder {
private OnSwipeListener onSwipeListener;
private int leftBackgroundColor;
private Bitmap leftIcon;
private int rightBackgroundColor;
private Bitmap rightIcon;
private float iconSize;
public Builder leftBackgroundColor(int leftBackgroundColor) {
this.leftBackgroundColor = leftBackgroundColor;
return this;
}
public Builder leftIcon(Bitmap leftIcon) {
this.leftIcon = leftIcon;
return this;
}
public Builder rightBackgroundColor(int rightBackgroundColor) {
this.rightBackgroundColor = rightBackgroundColor;
return this;
}
public Builder rightIcon(Bitmap rightIcon) {
this.rightIcon = rightIcon;
return this;
}
public Builder iconSize(float iconSize) {
this.iconSize = iconSize;
return this;
}
public Builder onSwipeListener(OnSwipeListener onSwipeLister) {
this.onSwipeListener = onSwipeLister;
return this;
}
public CustomItemTouchHelperCallback build() {
return new CustomItemTouchHelperCallback(this);
}
}
@@ -1,10 +1,16 @@
package com.messedcode.swipecollapseanimationexample;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.util.TypedValue;
public class MainActivity extends AppCompatActivity {
@@ -24,9 +30,39 @@ protected void onCreate(Bundle savedInstanceState) {
recyclerView.setLayoutManager(layoutManager);
// Add swipe
CustomItemTouchHelperCallback helperCb = new CustomItemTouchHelperCallback();
int red = Color.parseColor("#F44336");
int green = Color.parseColor("#4CAF50");
Drawable deleteIconWhite = getResources().getDrawable(R.drawable.ic_delete_black_24dp).mutate();
deleteIconWhite.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
Drawable checkIconWhite = getResources().getDrawable(R.drawable.ic_check_black_24dp).mutate();
checkIconWhite.setColorFilter(Color.WHITE, PorterDuff.Mode.SRC_IN);
float iconSizeInDp = 24;
CustomItemTouchHelperCallback helperCb = new CustomItemTouchHelperCallback.Builder()
.iconSize(dpToPx(iconSizeInDp))
.leftBackgroundColor(red)
.leftIcon(convertDrawableToBitmap(deleteIconWhite))
.rightBackgroundColor(green)
.rightIcon(convertDrawableToBitmap(checkIconWhite))
.build();
ItemTouchHelper helper = new ItemTouchHelper(helperCb);
helper.attachToRecyclerView(recyclerView);
}
private float dpToPx(float dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, getResources().getDisplayMetrics());
}
private Bitmap convertDrawableToBitmap(Drawable drawable) {
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
return bitmap;
}
}

0 comments on commit 43a45d1

Please sign in to comment.