Montoya
Montoya

Reputation: 3049

Android - Swipe to delete - adding an undo option

I am using a RecyclerView and I have managed to implement Swipe to Delete using this code:

  @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;
                    canvas = c;
                    Paint p = new Paint();
                    Bitmap icon;

                    if (dX > 0) {
                        icon = BitmapFactory.decodeResource(
                                activity.getResources(), R.drawable.ic_delete_white_36dp);
        /* Set your color for positive displacement */
                        p.setARGB(255, 255, 0, 0);
                        // Draw Rect with varying right side, equal to displacement dX
                        c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), dX,
                                (float) itemView.getBottom(), p);
                        // Set the image icon for Right swipe
                        c.drawBitmap(icon,
                                (float) itemView.getLeft() + HelperMethods.dpToPixel(16),
                                (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight()) / 2,
                                p);
                    } else {
                        icon = BitmapFactory.decodeResource(
                                activity.getResources(), R.drawable.ic_delete_white_36dp);

        /* Set your color for negative displacement */
                        p.setARGB(255, 255, 0, 0);

                        // Draw Rect with varying left side, equal to the item's right side
                        // plus negative displacement dX
                        c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(),
                                (float) itemView.getRight(), (float) itemView.getBottom(), p);

                        //Set the image icon for Left swipe
                        c.drawBitmap(icon,
                                (float) itemView.getRight() - HelperMethods.dpToPixel(16) - icon.getWidth(),
                                (float) itemView.getTop() + ((float) itemView.getBottom() - (float) itemView.getTop() - icon.getHeight()) / 2,
                                p);
                    }
                    Integer ALPHA_FULL = 255;
                    // Fade out the view as it is swiped out of the parent's bounds
                    final float alpha = ALPHA_FULL - Math.abs(dX) / (float) viewHolder.itemView.getWidth();
                    viewHolder.itemView.setAlpha(alpha);
                    viewHolder.itemView.setTranslationX(dX);
                }
            }

I am having trouble implementing UNDO option for few seconds after the user swiped an item.

gmail undo option

I want it to look similiar to gmail app undo option and i would like to not using any external library.

Upvotes: 3

Views: 3323

Answers (1)

Nemanja Kovacevic
Nemanja Kovacevic

Reputation: 3560

I'm guessing you have an onSwiped method where you remove the item from the adapter? My advice would be to not actually remove it there, just mark it as swiped off. Your adapter and view holder should present those views the same as your drawing behind in onChildDraw. You have an undo button there so if user taps it just set swiped off flag to false and redraw the row (I mean notify adapter to do it). In onSwiped start a timer and if nothing happens in x milliseconds actually remove the item from the adapter.

EDIT: this is just an idea, never done this actually...

EDIT: I had a go at this, see this blog post and this github repo.

Upvotes: 2

Related Questions