When we swipe from the end, I am able to display red colour background and a delete icon. I am trying to achieve partial swipe functionality, where on partial swipe, the swipe stops before the delete icon. I tried following these solutions. But, it didn't work for me. Please point me in the right direction.
public class PriorityItemTouchHelperCallback extends ItemTouchHelper.Callback {
private final ItemTouchHelperListener mAdapter;
private Context context;
public PriorityItemTouchHelperCallback(
ItemTouchHelperListener adapter, Context context) {
mAdapter = adapter;
this.context = context;
}
@Override
public boolean isLongPressDragEnabled() {
return true;
}
@Override
public boolean isItemViewSwipeEnabled() {
return true;
}
@Override
public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
final int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN;
final int swipeFlags = ItemTouchHelper.START;
return makeMovementFlags(dragFlags, swipeFlags);
}
@Override
public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
return mAdapter.onItemMove(viewHolder.getAdapterPosition(), target.getAdapterPosition());
}
@Override
public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
}
@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) {
// Get RecyclerView item from the ViewHolder
View itemView = viewHolder.itemView;
float height = (float) itemView.getBottom() - (float) itemView.getTop();
float width = height / 3;
Paint p = new Paint();
p.setColor(Color.RED);
if (dX > 0) {
c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
(float) itemView.getBottom(), p);
} else {
p.setColor(Color.RED);
RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(), (float) itemView.getRight(), (float) itemView.getBottom());
c.drawRect(background, p);
Bitmap icon = BitmapFactory.decodeResource(context.getResources(), R.drawable.round_delete_white_24);
RectF icon_dest = new RectF(
(float) itemView.getRight() - 2 * width + width / 2,
(float) itemView.getTop() + width + width / 8,
(float) itemView.getRight() - width / 2,
(float) itemView.getBottom() - width - width / 8);
c.drawBitmap(icon, null, icon_dest, p);
}
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
}
Change the dx parameter to dx/3 or dx/5 in this line:
super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
as dx parameter values are between (0 means no swipe) and (1 means full swipe)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With