-1

i Have a recyclerview list with 4 elements the swiping will be done sequentially how the swiped views get back to their i.e on top of list on pressing undo button every time one by one?

MyAdapter

public class RecyclerListAdapter extends RecyclerView.Adapter<RecyclerListAdapter.ItemViewHolder>
        implements ItemTouchHelperAdapter {

    private Card lastRemovedItem;
    private int lastIndex = -1;


    private final List<Card> mItems = new ArrayList<>();

    Card car1 = new Card(R.drawable.card1, "first");
    Card car2 = new Card(R.drawable.card2, "Second");
    Card car3 = new Card(R.drawable.card3, "Third");
    Card car4 = new Card(R.drawable.card4, "fourth");


    public RecyclerListAdapter() {

        mItems.addAll(Arrays.asList(car1, car2, car3, car4));
    }

    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(card, parent, false);
        ItemViewHolder itemViewHolder = new ItemViewHolder(view);
        return itemViewHolder;
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        Card card = mItems.get(position);
        holder.textView.setText(card.getText());
        holder.imageView.setImageResource(card.getImage());

    }

    @Override
    public void onItemDismiss(int position) {

        lastRemovedItem = mItems.get(position);
        lastIndex = position;
        mItems.remove(position);
        notifyItemRemoved(position);
    }


    public void restoreLastItem() {
        if (lastIndex == -1)
            return;

        mItems.add(lastIndex, lastRemovedItem);
        notifyItemInserted(lastIndex);
        lastRemovedItem = null;
        lastIndex = -1;
    }


    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Card prev = mItems.remove(fromPosition);
        mItems.add(toPosition > fromPosition ? toPosition - 1 : toPosition, prev);
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public int getItemCount() {
        return mItems.size();
    }

public static class ItemViewHolder extends RecyclerView.ViewHolder implements
            ItemTouchHelperViewHolder {

        public final TextView textView;
        public final ImageView imageView;

        public ItemViewHolder(View itemView) {
            super(itemView);
            textView = (TextView) itemView.findViewById(R.id.heading_text);
            imageView = (ImageView) itemView.findViewById(R.id.image);

        }

i tried using saving swiped item on onItemDismiss method and calling restoreLastItem but this just works for the last swiped view how can i get the views before that back on List.

Amit Singh
  • 23
  • 4

1 Answers1

0

As I said in your last question, note all removed cards then add them back in exactly the same way.

You can use a stack for this. The stack keeps a copy of each removed card which you can restore in the order they were added to the stack by calling pop.

You will of course need to do some error checking to ensure the stack is not empty.

e.g

private Stack<Card> removedItems = new Stack<Card>();


private void Remove() {

    removedItems.push(cardToRemove);

}

private void Restore() {

    Card cardToRestore = removedItems.pop()

}
Kuffs
  • 35,581
  • 10
  • 79
  • 92