title: RecyclerView, Inconsistency detected: Update items during scrolling
body: <p>Im trying to replace my <code>CustomListView</code> with <code>RecyclerView</code>, but i am facing an issue calls <code>IndexOutOfBoundsException: Inconsistency detected</code>.  </p>
  
  <p>I use application service in order to update/change my entire list items every 10 seconds by getting data from server. But my RecyclerView crash everytime, when service making request, update contents and scrolling at the same time.</p>
  
  <p>I know that this question has been already asked, but i still don't get an correct answer that solves my problem. So I decided to ask you guys again for help, before I go back to my <code>CustomListView</code>.</p>
  
  <p>I have tried almost every single answer on <a href="https://stackoverflow.com/questions/26827222/inconsistency-detected-in-recyclerview-how-to-change-contents-of-recyclerview-w">this</a> and <a href="https://code.google.com/p/android/issues/detail?id=77846" rel="nofollow noreferrer">this</a> posts, but still no success. </p>
  
  <p>Here is my Adapter:</p>
  
  <pre><code>public class EventAdapter extends RecyclerView.Adapter&lt;EventAdapter.MyViewHolder&gt; {
  
      private List&lt;EventModel&gt; eventList = new ArrayList&lt;&gt;();
      private Context context;
      public class MyViewHolder extends RecyclerView.ViewHolder {
         private TextView timeView, titleView;
  
          public MyViewHolder(View view) {
              super(view);
              titleView = (TextView) view.findViewById(R.id.txtListTitle);
              timeView = (TextView) view.findViewById(R.id.txtListTime);
          }
      }
  
  
      public EventAdapter(Context context, List&lt;EventModel&gt; eventList) {
          this.eventList = eventList;
          this.context = context;
      }
  
      @Override
      public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View itemView = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.list_event_form, parent, false);
  
          return new MyViewHolder(itemView);
      }
  
      @Override
      public void onBindViewHolder(MyViewHolder holder, int position) {
          EventModel event = eventList.get(position);
          holder.titleView.setText(event.getTitle());
          holder.timeView.setText(event.getEndTime());
      }
  
      /*public void update(ArrayList&lt;EventModel&gt; list) {
  
          if (eventList != null) {
              eventList.clear();
              eventList.addAll(0,list);
          }
          else {
              eventList = list;
          }
          notifyDataSetChanged(); // this does not work
      }*/
  
      @Override
      public int getItemCount() {
          return eventList.size();
      }
  }
  </code></pre>
  
  <p>Here is where i use RecyclerView:</p>
  
  <pre><code>    public class EventListFragment extends Fragment {
      private View view;
  
      private RecyclerView recyclerView;
      private LinearLayoutManager mLayoutManager;
      private EventAdapter eventAdapter;
      private ArrayList&lt;EventModel&gt; eventList;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
                               Bundle savedInstanceState) {
          view = inflater.inflate(R.layout.fragment_event_list, container, false);
          return view;
      }
  
      @Override
      public void onResume() {
          super.onResume();
          setView();
          setControl();
      }
  
      private void setView() {
          recyclerView = (RecyclerView) view.findViewById(R.id.frg_recycler_view);
      }
  
      private void setControl() {
            if (eventAdapter == null &amp;&amp; mLayoutManager == null) {
              eventList = new ArrayList&lt;&gt;();
              eventAdapter = new EventAdapter(getActivity().getApplicationContext(), eventList);
              mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
              recyclerView.setLayoutManager(mLayoutManager);
              recyclerView.setHasFixedSize(true);
              recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), LinearLayoutManager.VERTICAL));
              recyclerView.setAdapter(eventAdapter);
          }
  
      public void eventToList() { // this will be called by service every 10 secs
          final RequestData requestData = new RequestData(getActivity());
          eventList.clear(); // clean data, before inserting new data
          requestData.getEventToday(new RequestData.VolleyCallback() {
              @Override
              public void onSuccess(JSONObject result) {
                  for (int i = 0; i &lt; result.length(); i++) {
                      try {
                          JSONObject item = result.getJSONObject(Integer.toString(i));
                          EventModel eventModel = new EventModel();
                          String title = item.getString("title");
                          String date = item.getString("date");
  
                          eventModel.setTitle(title);
                          eventModel.setDate(date);
  
                          eventList.add(eventModel);
                      } catch (JSONException e) {
                          Log.d(TAG, "error" + e.toString());
                          e.printStackTrace();
                      }
                  }
                     // Log.d(TAG, "onSuccess: Update Currentdate");
                      eventAdapter.notifyDataSetChanged();
              }
          });
      }
  }
  </code></pre>
  
  <p>And here is what I have got:</p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
                Process: com.example.longluong.mybbs, PID: 10469
                java.lang.IndexOutOfBoundsException: Inconsistency detected. Invalid view holder adapter positionViewHolder{1b43c6c position=10 id=-1, oldPos=-1, pLpos:-1 no parent}
                    at android.support.v7.widget.RecyclerView$Recycler.validateViewHolderForOffsetPosition(RecyclerView.java:5249)
                    at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5431)
                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5392)
                    at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5388)
                    at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2149)
                    at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1533)
                    at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1496)
                    at android.support.v7.widget.LinearLayoutManager.scrollBy(LinearLayoutManager.java:1313)
                    at android.support.v7.widget.LinearLayoutManager.scrollVerticallyBy(LinearLayoutManager.java:1058)
                    at android.support.v7.widget.RecyclerView.scrollByInternal(RecyclerView.java:1680)
                    at android.support.v7.widget.RecyclerView.onTouchEvent(RecyclerView.java:2837)
                    at android.view.View.dispatchTouchEvent(View.java:9302)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2548)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2241)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2554)
                    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2255)
                    at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2403)
                    at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1737)
                    at android.app.Activity.dispatchTouchEvent(Activity.java:2805)
                    at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:71)
                    at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2364)
                    at android.view.View.dispatchPointerEvent(View.java:9522)
                    at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4230)
                    at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4096)
                    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                    at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3787)
                    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                    at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3844)
                    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                    at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3695)
                    at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3661)
                    at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3669)
                    at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3642)
                  at android.view.ViewRootImpl.deliverInput
  </code></pre>