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.
foregroundoverlaps thebackgroundeven when shifted. I would try to make it inactive after the svayp in the onSwiped method. - kulikovman