title: correct way to implement item touch helper on recyclerview in android
body: <p>i followed a tutorial <a href="https://medium.com/@ipaulpro/drag-and-swipe-with-recyclerview-b9456d2b1aaf#.bgvghid2o" rel="nofollow">here</a> on how to implement onItemTouchHelper and callback methods when i run my code it works to the point that i can long press and pick up the item but the moment i move it past one of the other items in the list my app will force close with an index out of bounds exception can anybody here help me?</p>
  
  <p>here is my code but it is identical to the code in the tutorial,</p>
  
  <p>Simple Item Touch Helper Callback</p>
  
  <pre><code>public class SimpleItemTouchHelperCallback extends ItemTouchHelper.Callback 
  {
  
  private final predictsCardAdapter mAdapter;
  
  public SimpleItemTouchHelperCallback(predictsCardAdapter adapter){
      mAdapter = adapter;
  }
  
  @Override
  public boolean isLongPressDragEnabled(){
      return true;
  }
  @Override
  public boolean isItemViewSwipeEnabled(){
      return false;
  }
  
  @Override
  public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
      int dragFlags = ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
      int swipeFlags = ItemTouchHelper.START | ItemTouchHelper.END;
  
      return makeMovementFlags(dragFlags,swipeFlags);
  }
  
  @Override
  public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
      mAdapter.onItemMove(viewHolder.getAdapterPosition(), 
      target.getAdapterPosition());
      //mAdapter.notifyItemMoved(viewHolder.getAdapterPosition(), 
      //target.getAdapterPosition());
      return true;
  }
  
  @Override
  public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {
  // mAdapter.onItemDismiss(viewHolder.getAdapterPosition());
  }
  }
  </code></pre>
  
  <p>heres my adapter</p>
  
  <pre><code>public class predictsCardAdapter extends   
  RecyclerView.Adapter&lt;predictsCardAdapter.MyViewHolder&gt; implements    
  ItemTouchHelperAdapter{
  private List&lt;addNewCard&gt; cardMakerList;
  
  @Override
  public boolean onItemMove(int fromPosition, int toPosition) {
      if (fromPosition &lt; toPosition){
          for (int i = fromPosition; i &lt; toPosition; i++){
              Collections.swap(cardMakerList,i,i+1);
          }
      }else{
          for (int i = fromPosition; i &gt; toPosition; i--){
              Collections.swap(cardMakerList,i,i-1);
          }
      }
      notifyItemMoved(fromPosition,toPosition);
      return true;
  }
  
  @Override
  public void onItemDismiss(int position) {
  cardMakerList.remove(position);
      notifyItemRemoved(position);
  }
  
  public class MyViewHolder extends RecyclerView.ViewHolder{
      public TextView cardText, speechText;
      public ImageView cardImage;
      public ContentLoadingProgressBar progress;
  
      public MyViewHolder(View view){
          super(view);
          cardText = (TextView) view.findViewById(R.id.predictscardText);
          speechText = (TextView) view.findViewById(R.id.predictsspeechText);
          cardImage = (ImageView) view.findViewById(R.id.predictscardimage);
          progress = (ContentLoadingProgressBar)  
          view.findViewById(R.id.predictsprogress);
      }
  
  }
  public predictsCardAdapter(List&lt;addNewCard&gt; cardMakerList){
      this.cardMakerList = cardMakerList;
  }
  @Override
  public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
      View itemView = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.predicted_card, parent, false);
      return new MyViewHolder(itemView);
  }
  @Override
  public void onBindViewHolder 
  (final MyViewHolder predicts_holder, int position) {
      addNewCard cardmaker = cardMakerList.get(position);
      predicts_holder.cardText.setText(cardmaker.getCardName());
      predicts_holder.speechText.setText(cardmaker.getCardName());
      //Drawable d = new  
      BitmapDrawable(Utility.getPhoto(cardmaker.getCardIcon()));
  
      String url = cardmaker.getCardIcon();
      ImageLoader imageLoader = ImageLoader.getInstance();
      DisplayImageOptions options = new  
      DisplayImageOptions.Builder().cacheInMemory(true)
              .cacheOnDisc(true).resetViewBeforeLoading(true)
              .showImageForEmptyUri(R.drawable.loading_image)
              .showImageOnFail(R.drawable.broken_image).build();
  
      imageLoader.displayImage
      (url, predicts_holder.cardImage, options, new  
      SimpleImageLoadingListener() {
          @Override
          public void onLoadingStarted(String imageUri, View view) {
              predicts_holder.progress.setVisibility(View.VISIBLE);
          }
  
          @Override
          public void onLoadingFailed
          (String imageUri, View view, FailReason failReason) {
              predicts_holder.progress.setVisibility(View.GONE);
          }
  
          @Override
          public void onLoadingComplete
      (String imageUri, View view, Bitmap loadedImage) {
              predicts_holder.progress.setVisibility(View.GONE);
          }
      });
      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
  predicts_holder.cardText.setTextAppearance
  (android.R.style.TextAppearance_Material_Small);
      }
      else{
          predicts_holder.cardText.setTextAppearance
  (predicts_holder.cardImage.getContext(    ), 
  android.R.style.TextAppearance_Material_Small);
      }
      predicts_holder.cardText.setTextSize(10);
  }
  @Override
  public int getItemCount(){
      return cardMakerList.size();
  }
  }
  </code></pre>
  
  <p>i also have a ItemTouchHelperAdapter</p>
  
  <pre><code>public interface ItemTouchHelperAdapter {
  boolean onItemMove(int fromPosition, int toPosition);
  void onItemDismiss(int position);
  }
  </code></pre>
  
  <p>and i use it in my activity like this </p>
  
  <pre><code> ItemTouchHelper.Callback callback = new     
   SimpleItemTouchHelperCallback(predicts_card_adapter);
      ItemTouchHelper touchHelper = new ItemTouchHelper(callback);
      touchHelper.attachToRecyclerView(recyclerView);
  </code></pre>
  
  <p>the error i get is </p>
  
  <pre><code>java.lang.IndexOutOfBoundsException
  
  at java.util.Collections.swap(Collections.java:1937)
  
  at 
  
  ss.sealstudios.com.socialstories.predictsCardAdapter.
  onItemMove(predictsCardAdapter.java:33)
  
  at ss.sealstudios.com.socialstories.SimpleItemTouchHelperCallback
  .onMove(SimpleItemTouchHelperCallback.java:36)
  </code></pre>
  
  <p>in the onMove methods if i uncomment the   </p>
  
  <pre><code>mAdapter.onItemMove(viewHolder.getAdapterPosition(),   
  target.getAdapterPosition());
  </code></pre>
  
  <p>the drag and drop half works so i can drag it but not drop it and the views dont move to accomodate it
  which makes me think its my cardMaker list but that is just a list from a database, can anybody see something i cant?</p>