0

I implemented a swipe left to delete action in my recycler view like below:

ItemTouchHelper

public class SwipeToDelete extends ItemTouchHelper.SimpleCallback {

    //my custom adapter
    private MyAdapter slAdapter;

    public SwipeToDelete(MyAdapter adapter){
       // TO only swipe to the left
        super(0, ItemTouchHelper.LEFT);
        slAdapter = adapter;
    }

    @Override
    public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
        int position = viewHolder.getAdapterPosition();
        slAdapter.deleteItem(position);
    }
}

MyAdapter

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MYViewHolder> {

   //... onCreateViewHolder .. etc

   //creating a list of my custom layout view
   private List<MyCustomCardView> cards;

   public void deleteItem(int position){
        cards.remove(position);
        notifyItemRemoved(position);
    }
}

With simply that, I am able to swipe and delete views in my recyclerView.

What I would like to do is to stop the swipe halfway and show an icon. The user can then either click the icon and the swipe continues and deletes the view, or else continues the swipe himself/herself and the view will be deleted.

As it is right now, the user can just barely swipe left and the swipe automatically continues, I would like to have an intermediary step which shows an icon that is clickable. How can one do that with the above code?

bcsta
  • 1,963
  • 3
  • 22
  • 61

1 Answers1

0

you can use extra boolean field in your model (eg: isShowRemoveButton) and instead of "cards.remove(position)" set it to true (eg: cards.get(position).setShowRemoveButton(true) ) and in your onBindViewHolder handle it to show delete button on your items layout

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MYViewHolder> {

       //... onCreateViewHolder .. etc

    private List<MyCustomCardData> cards;//CardData instead of CardView

    public void deleteItem(int position){
            cards.get(position).setShowRemoveButton(true) ;
            notifyItemRemoved(position);
        }

    public void onBindViewHolder(myViewHolder holder, int position) {

            MyCustomCardData currentdata = cards.get(position);
            holder.removeButton.setVisibility(currentdata.isShowRemoveButton? VISIBLE : GONE);
    }
}
taha
  • 731
  • 2
  • 7
  • 18
  • I would appreciate a more comprehensive answer no need for full code just important snippets on how to implement this – bcsta Aug 09 '19 at 09:18
  • I think you have to take a list of your data objects rather than list of your custom layout view – taha Aug 09 '19 at 15:31