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.