I.d007
I.d007

Reputation: 156

Swipe card view undo card item in Android

I need some help. I am using swipe card library like Tinder app in Android and I am removing the cards by using cross button and Now I want undo options also on the click of back option. Can anyone please tell me what is the method used for that ? I really appreciate that. Thanks!

Upvotes: 2

Views: 1425

Answers (2)

adgvcxz
adgvcxz

Reputation: 341

I wrote a library some time ago, has achieved this function, I hope to help you https://github.com/adgvcxz/CardLayoutManager

Upvotes: 1

user1736049
user1736049

Reputation: 366

I assume that you have implemented an ÌtemTouchHelper.SimpleCallback interface to control the swiping. I've included an example of such an implementation below:

ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) {

            @Override
            public void onSwiped(RecyclerView.ViewHolder viewHolder, int swipeDir) {

                final int adapterPosition = viewHolder.getAdapterPosition();
                final Task taskItem = tasks.remove(adapterPosition); //tasks is an ArrayList of data that populates the RecyclerView. This line deletes an element from the ArrayList, but at the same time saves it in a variable.
                adapter.notifyItemRemoved(viewHolder.getAdapterPosition()); //adapter represents my adapter class.

                Snackbar snackbar = Snackbar    //assuming that a Snackbar with "UNDO" button is what you want. 
                        .make(rvTasks, "Item removed", Snackbar.LENGTH_LONG)
                        .setAction("Undo", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                //this happens when "Undo" is clicked. 
                                tasks.add(adapterPosition, taskItem);
                                adapter.notifyItemInserted(adapterPosition);
                                rvTasks.scrollToPosition(adapterPosition);
                            }
                        });
                snackbar.show();
            }

            @Override
            public void onMoved(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, int fromPos, RecyclerView.ViewHolder target, int toPos, int x, int y) {
                super.onMoved(recyclerView, viewHolder, fromPos, target, toPos, x, y);
            }

            @Override
            public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
                return false;
            }
        };

        ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleItemTouchCallback);
        itemTouchHelper.attachToRecyclerView(rvTasks);

Sequence:

final int adapterPosition = viewHolder.getAdapterPosition(); 
//saves the position of card/item in case we want to reinstate it.
final Task taskItem = tasks.remove(adapterPosition); 
//deletes an element from the ArrayList, but at the same time saves it in a variable.
adapter.notifyItemRemoved(viewHolder.getAdapterPosition()); 
//adapter is notified that item is removed and thus removed from view.

then,

Snackbar.make(... details omitted ...) //remember to add in your build.gradle: compile 'com.android.support:design:25.3.1'
tasks.add(adapterPosition, taskItem); //deleted element readded to ArrayList
adapter.notifyItemInserted(adapterPosition); //adapter notified of reinstated item
rvTasks.scrollToPosition(adapterPosition); //auto-scrolls to position where item is reinstated

Upvotes: 1

Related Questions