How to restore swiped item from RecyclerView after dialog show?

I am using ItemTouchHelper with AlertDialog. The problem is when user swipe the element to the right side. AlertDialog with EditText is show at the screen. Now I want to do something like that:
-When user press "save" button the element disappears not removed, only "go out" from whole RecyclerView list.
-When "cancel" button is pressed the element should return to the same place that it was before (It was be pretty that it will be the animation for mAdapter.notifyItemChanged(); or mAdapter.notifyItemInserted();
So I tried two sollutions but without success.

itemTouchHelper = new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0,
                ItemTouchHelper.RIGHT) {
            @Override
            public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
                return false;
            }
            @Override
            public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
                    position = viewHolder.getAbsoluteAdapterPosition(); // 2. Sollution
                    openDialog();
                    vieholder2 = viewHolder; // 1. Sollution
            }
        });
        itemTouchHelper.attachToRecyclerView(recyclerView);

First I tried implemented my own interface and after user press "cancel" button interface should be invoke and set code below:
itemTouchHelper.startSwipe(vieholder2);
Second:
After interface is invoked I set mAdapter.notifyItemChanged(position);
Both sollutions works good, directly in onSwiped method in ItemTouchHelper, but after method was triggered and Dialog is showing. Sollutions aren’t working.
So how can I return the invisible item back to the RecyclerView list after "cancel" button on Dialog is clicked?
I am using LiveData if it is helpful.

Source: Android Questions

LEAVE A COMMENT