Created
March 6, 2018 09:15
-
-
Save wakwak3125/347d5bbdaa72810283ad3b9ef35e7b43 to your computer and use it in GitHub Desktop.
SwipeToDeleteCallback.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Ryo on 2018/03/06. | |
*/ | |
public abstract class SwipeToDeleteCallback extends ItemTouchHelper.SimpleCallback { | |
private ColorDrawable mBackgroundColorDrawable = new ColorDrawable(); | |
private Drawable mSwipeBackIcon; | |
public SwipeToDeleteCallback(@NonNull Drawable swipeIcon, @ColorInt int swipeBackgroundColor) { | |
super(0, ItemTouchHelper.LEFT); | |
mSwipeBackIcon = swipeIcon; | |
mBackgroundColorDrawable.setColor(swipeBackgroundColor); | |
} | |
@Override | |
public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { | |
View itemView = viewHolder.itemView; | |
int itemViewHeight = itemView.getBottom() - itemView.getTop(); | |
mBackgroundColorDrawable.setBounds( | |
itemView.getRight() + ((int) dX), | |
itemView.getTop(), | |
itemView.getRight(), | |
itemView.getBottom() | |
); | |
mBackgroundColorDrawable.draw(c); | |
int top = itemView.getTop() + (itemViewHeight - mSwipeBackIcon.getIntrinsicHeight()) / 2; | |
int deleteIconMargin = (itemViewHeight - mSwipeBackIcon.getIntrinsicHeight()) / 2; | |
int left = itemView.getRight() - deleteIconMargin - mSwipeBackIcon.getIntrinsicWidth(); | |
int right = itemView.getRight() - deleteIconMargin; | |
int bottom = top + mSwipeBackIcon.getIntrinsicHeight(); | |
mSwipeBackIcon.setBounds(left, top, right, bottom); | |
mSwipeBackIcon.draw(c); | |
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); | |
} | |
@Override | |
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment