0

I override in SimpleItemTouchHelper class the method onChildDraw the following way:

@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) {
        float width = (float) viewHolder.itemView.getWidth();
        float alpha = 1.0f - Math.abs(dX) / width;
        viewHolder.itemView.setAlpha(alpha);
        viewHolder.itemView.setTranslationX(dX);
    } else {
        super.onChildDraw(c, recyclerView, viewHolder, dX, dY,
                actionState, isCurrentlyActive);
    }
}

This simply enables a fade in animation when you swipe to left or right .

In my adapter, I've implemented the ItemTouchHelperAdapter and override onItemDismiss the following way in order to show an AlertDialog to confirm if you want to remove the selected element:

public class TeambuilderAdapter extends RecyclerView.Adapter<TeambuilderAdapter.ViewHolder> implements ItemTouchHelperAdapter {

    @Override
    public void onItemDismiss(int position) {

        DialogInterface.OnClickListener dialogClickListener =(dialog, which) -> {
            if (which == DialogInterface.BUTTON_POSITIVE) {
                mData.remove(position);
                notifyItemRemoved(position);
                ((PokemonTeambuilderTabActivity) mContext).refreshTeamList();
            }
        };

        new AlertDialog . Builder (mContext)
                .setTitle(mContext.getString(R.string.remove_team))
                .setMessage(mContext.getString(R.string.remove_team_question))
                .setPositiveButtonIcon(ContextCompat.getDrawable(mContext, R.drawable.ic_baseline_check_circle_14))
                .setPositiveButton(mContext.getString(R.string.confirm), dialogClickListener)
                .setNegativeButtonIcon(ContextCompat.getDrawable(mContext, R.drawable.ic_baseline_cancel_14))
                .setNegativeButton(mContext.getString(R.string.decline), dialogClickListener)
                .show();

    }
...
}

All is working, the problem resides that if I press the cancel button, the item swiped disappears and only appears again when I re-enter the activity.

How can I restore the original position when I press cancel or I leave the dialog?

Jenea Vranceanu
  • 4,530
  • 2
  • 18
  • 34
Nexussim Lements
  • 535
  • 1
  • 15
  • 47

1 Answers1

1

Your dialogClickListener is not handling the cancel button action.
For the cancel action, get your itemView and setTranslationX(0) and setAlpha(1).

Jens Zalzala
  • 2,546
  • 1
  • 22
  • 28
  • How can I get the itemView in my cancel button action?? – Nexussim Lements Oct 31 '20 at 13:59
  • Well if you don't want to go through the trouble of looking that up, I think calling notifyItemChanged (int position) on your adapter will update the view at that position, so as long as you reset the alpha and translation when you bind the view, you should be good. – Jens Zalzala Nov 01 '20 at 14:18