title: java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{5280f810 position=0 id=-1, oldPos=0,
body: <p>There is something wrong with the below methods, but I do not know how to fix it:</p>
  
  <pre><code>mDataSet.remove(position - 1);
  notifyItemRemoved(position);
  notifyItemRangeChanged(0, getItemCount());
  </code></pre>
  
  <p>Entire class:</p>
  
  <pre><code>public class DemoAdapter extends RecyclerView.Adapter&lt;RecyclerView.ViewHolder&gt; implements View.OnClickListener, View.OnLongClickListener {
  
      private static final int TYPE_HEADER = 0;
      private static final int TYPE_NORMAL = 1;
      private Context mContext;
      private List&lt;String&gt; mDataSet;
  
      private MyViewHolder normalHolder;
      private HeaderHolder headerHolder;
  
      public DemoAdapter(List&lt;String&gt; mDataSet, Context context) {
          this.mContext = context;
          this.mDataSet = mDataSet;
      }
  
      public void setData(List&lt;String&gt; mDataset) {
          this.mDataSet = mDataset;
          notifyDataSetChanged();
      }
  
      public void Remove(int position) {
          mDataSet.remove(position);
          notifyItemRemoved(position);
      }
  
      public void Add(String text, int position) {
          mDataSet.add(position, text);
          notifyItemInserted(position);
      }
  
      @Override
      public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          Context context = parent.getContext();
          if (viewType == TYPE_HEADER) {
              //inflate header layout
              View headerview = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_main_recyclerview_headerview, parent, false);
              headerHolder = new HeaderHolder(headerview, mContext);
              return headerHolder;
  
          } else if (viewType == TYPE_NORMAL) {
              View normalView = LayoutInflater.from(context).inflate(R.layout.activity_main_recyclerview_itemview, parent, false);
              normalHolder = new MyViewHolder(normalView, mContext);
              return normalHolder;
          }
  
          // set the view's size, margins, paddings and layout parameters
          //return viewHolder;
          throw new RuntimeException("there is no type that matches the type " + viewType + " + make sure your using types correctly");
      }
  
      @Override
      public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
  
          LogUtils.I("COCO", " bind position:" +  position );
  
          if (holder instanceof HeaderHolder) {
              //test headerview's click listener happens correctly
              headerHolder = (HeaderHolder) holder;
  
              headerHolder.image.setOnClickListener(this);
  
          } else if (holder instanceof MyViewHolder) {
              //to avoid outofindexexception, as header size = 1
              String data = mDataSet.get(position - 1);
              normalHolder = (MyViewHolder) holder;
  
              normalHolder.mTextview.setOnClickListener(this);
              normalHolder.ll_itemView.setOnClickListener(this);
              normalHolder.ll_itemView.setOnLongClickListener(this);
              normalHolder.mTextview.setOnLongClickListener(this);
  
              normalHolder.mTextview.setTag(position);
              normalHolder.ll_itemView.setTag(position);
  
              normalHolder.mTextview.setText(data);
  
          }
      }
  
      @Override
      public int getItemCount() {
          if (null != mDataset &amp;&amp; mDataset.size() &gt; 0)
              //return mDataSet.size();
              //+1 is header's size
              return mDataSet.size() + 1;
  
          return 0;
      }
  
      @Override
      public int getItemViewType(int position) {
          if (position == 0)
              return TYPE_HEADER;
  
          return TYPE_NORMAL;
      }
  
      @Override
      public boolean onLongClick(View view) {
  
          switch (view.getId()) {
              case R.id.activity_main_recyclerview_itemview:
                  //click to remove itemview, as existing headerview so position -1
                  int position = (int) view.getTag();
                  mDataSet.remove(position - 1);
                  notifyItemRemoved(position);
                  notifyItemRangeChanged(0, getItemCount());
  
                  LogUtils.I("COCO", "data position:" +  view.getTag() );
                  LogUtils.I("COCO", " item position:" +  position );
                  break;
  
              case R.id.activity_main_recyclerview_itemview_textview:
                  LogUtils.I("COCO", "long click content:" + normalHolder.mTextview.getText());
                  break;
          }
          return true;
      }
  }
  </code></pre>