The project uses a RecyclerView for which you want to implement the element shift behavior on the swipe. Under the element is a delete button.

layout for the element looks like this

<layout> <background/> <foreground/> </layout> 

define callback

 public class MovingForwgroundTouchHelper extends ItemTouchHelper.SimpleCallback { public MovingForwgroundTouchHelper(int dragDirs, int swipeDirs) { super(dragDirs, swipeDirs); } @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return false; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { } @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) { if (viewHolder != null && viewHolder instanceof MovingForegroundViewHolder) { MovingForegroundViewHolder vh = (MovingForegroundViewHolder) viewHolder; final View foregroundView = vh.getForeground(); final float maxOffset = vh.getMaxOffset(); final float screenWidth = SystemBarTintManager.screenWidth(recyclerView.getContext()); getDefaultUIUtil().onDraw(c, recyclerView, foregroundView, dX * (maxOffset / screenWidth), dY, actionState, isCurrentlyActive); } } else { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } } } 

I shove it to the recycler

  ItemTouchHelper.SimpleCallback itemTouchHelperCallback = new MovingForwgroundTouchHelper(0, ItemTouchHelper.LEFT); new ItemTouchHelper(itemTouchHelperCallback).attachToRecyclerView(mRecyclerView); 

As a result, the shift works perfectly, but when I click on the item (in any place, including when I click on the button for which the listener is assigned to delete the item), the shifted foreground goes backwards and the deletion does not occur.

This is my first time working with ItemTouchHelper, maybe I’m missing something obvious. Please tell me what you need to add to this scheme so that it works.

I do not need the code that does this, but I need good advice on the theory, because for now I just don’t know in which direction to dig. I can’t give you the full project code, but specify, it might be possible to add some points.

  • There it seems that the foreground overlaps the background even when shifted. I would try to make it inactive after the svayp in the onSwiped method. - kulikovman
  • And when I was engaged in this, I came to the conclusion that for one action (one button) it is better to use an ordinary swipe with drawing a button without layouts. This solves most problems. - kulikovman
  • @kulikovman about overlapping option disappears, I hung OnClickListener on the foreground with the log, it does not work. Not really understand what you mean in the second version. Something like drawing an imitation button on the canvas and practicing a click on certain coordinates of the recycler? - iamthevoid
  • as I understand it, this is called swipe-to-dismiss . There, the desired action is performed in onSwiped , depending on the direction of the svayp. Instead of a button, the background is filled in onChildDraw and an explanatory icon or an inscription is drawn. Here is an example - learn2crack.com/2016/02/custom-swipe-recyclerview.html - kulikovman
  • @kulikovman thank you very much - iamthevoid

0