package com.sqisland.android.recyclerview;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

public class AddRemoveNumberedAdapter extends RecyclerView.Adapter<TextViewHolder> {
  private static final int ITEM_VIEW_TYPE_ITEM = 0;
  private static final int ITEM_VIEW_TYPE_ADD = 1;

  private List<String> labels;

  public AddRemoveNumberedAdapter(int count) {
    labels = new ArrayList<String>(count);
    for (int i = 0; i < count; ++i) {
      labels.add(String.valueOf(i));
    }
  }

  @Override
  public TextViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(
        viewType == ITEM_VIEW_TYPE_ADD ? R.layout.item_add : R.layout.item, parent, false);
    return new TextViewHolder(view);
  }

  @Override
  public void onBindViewHolder(final TextViewHolder holder, final int position) {
    if (position == labels.size()) {
      holder.textView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
          addItem();
        }
      });
      return;
    }

    final String label = labels.get(position);
    holder.textView.setText(label);
    holder.textView.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        removeItem(holder.getPosition());
      }
    });
  }

  private void addItem() {
    int lastItem = Integer.parseInt(labels.get(labels.size() - 1));
    labels.add(String.valueOf(lastItem + 1));
    notifyItemInserted(labels.size() - 1);
  }

  private void removeItem(int position) {
    labels.remove(position);
    notifyItemRemoved(position);
  }

  @Override
  public int getItemViewType(int position) {
    return position == labels.size() ? ITEM_VIEW_TYPE_ADD : ITEM_VIEW_TYPE_ITEM;
  }

  @Override
  public int getItemCount() {
    return labels.size() + 1;
  }
}
@@ -180,37 +180,8 @@
 oup;
-%0Aimport android.widget.Toast;
 %0A%0Aim
@@ -790,16 +790,20 @@
         
+    
 viewType
@@ -1392,24 +1392,26 @@
 r() %7B%0A      
+  
 @Override%0A  
@@ -1404,24 +1404,26 @@
   @Override%0A
+  
       public
@@ -1454,16 +1454,20 @@
         
+ 
+   
 removeIt
@@ -1494,24 +1494,26 @@
 n());%0A      
+ 
+ 
 %7D%0A    %7D);%0A  
@@ -1538,24 +1538,56 @@
 addItem() %7B%0A
+    if (labels.size() %3E=1)%7B%0A    
     int last
@@ -1642,24 +1642,28 @@
  - 1));%0A    
+  
+  
 labels.add(S
@@ -1688,24 +1688,28 @@
 Item + 1));%0A
+    
     notifyIt
@@ -1739,16 +1739,104 @@
 ) - 1);%0A
+    %7D else %7B%0A        labels.add(new String(%220%22));%0A        notifyItemInserted(0);%0A    %7D%0A%0A
   %7D%0A%0A  p

Fix crash when adding a new item after removing everything