title: RecyclerView Slide New Item In At Top
body: <p>I recently set up my RecyclerView and want to slide in all new items that are added. Specifically, I have a comment screen and every time a new comment is added, I want it to slide in from the left with an animation effect.</p>
  
  <p>Note that I add all new items to my <code>mCommentArrayList</code> at <code>index 0</code> so that they appear at the top of the view.</p>
  
  <p>I have read about using third-party libraries, but from what I understand that is not necessary. Here is my code:</p>
  
  <p>Where I first call to Firebase to find data to populate the RecyclerView:</p>
  
  <pre><code>    mUpdateRef.addListenerForSingleValueEvent(new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              setImage(dataSnapshot);
              setQuestion(dataSnapshot);
              createInitialCommentIDArray(dataSnapshot);
              mNumberOfCommentsAtPoll = (int) dataSnapshot.child(COMMENTS_LABEL).getChildrenCount();
              for (int i = 0; i &lt; mNumberOfCommentsAtPoll; i++) {
                  String commentID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("COMMENT").getValue();
                  Log.v("COMMENT_ID", "The comment ID is " + commentID);
                  String userID = (String) dataSnapshot.child(COMMENTS_LABEL).child(mCommentIDArrayList.get(i)).child("USER_ID").getValue();
                  Log.v("USER_ID", "The user ID is " + userID);
                  mCommentArrayList.add(0, new Comments(mUserAvatar, userID, commentID));
                  mCommentAdapter.notifyDataSetChanged();
              }
  
          }
  
          @Override
          public void onCancelled(FirebaseError firebaseError) {
  
          }
      });
  </code></pre>
  
  <p>Subsequent Calls to Firebase on Data Change:</p>
  
  <pre><code>   @Override
  protected void onStart() {
      super.onStart();
      mUpdateComments = new ValueEventListener() {
          @Override
          public void onDataChange(DataSnapshot dataSnapshot) {
              mNumberOfCommentsAtPoll = (int) dataSnapshot.getChildrenCount();
              for (DataSnapshot x : dataSnapshot.child(COMMENTS_LABEL).getChildren()) {
                  Log.v("DATA_SNAPSHOT", x.toString());
                  if (mCommentIDArrayList.contains(x.getKey())) {
                      Log.v("Comment_Already_Added", x.getKey());
                  } else {
                      Log.v("Child_Added_Called", "Child Added Called");
                      mCommentIDArrayList.add(x.getKey());
                      String commentID = (String) dataSnapshot.child(COMMENTS_LABEL).child(x.getKey()).child("COMMENT").getValue();
                      Log.v("New_Comment", "The new comment is " + commentID);
                      String userID = (String) dataSnapshot.child(COMMENTS_LABEL).child(x.getKey()).child("USER_ID").getValue();
                      Log.v("New_User_ID", "The new userID is " + userID);
                      mCommentArrayList.add(0, new Comments(mUserAvatar, userID, commentID));
                      mCommentAdapter.notifyDataSetChanged();
                  }
              }
          }
  </code></pre>
  
  <p>My RecyclerView Adapter:</p>
  
  <pre><code>public class MyAdapter extends RecyclerView.Adapter&lt;MyAdapter.ViewHolder&gt; {
  
      private ArrayList&lt;Comments&gt; mDataSet;
  
  
      // Provide a reference to the views for each data item
      // Complex data items may need more than one view per item, and
      // you provide access to all the views for a data item in a view holder
      public class ViewHolder extends RecyclerView.ViewHolder {
          // each data item is just a string in this case
          protected ImageView userAvatar;
          protected TextView userID;
          protected TextView userComment;
  
          public ViewHolder(View v) {
              super(v);
              userAvatar = (ImageView) v.findViewById(R.id.profile_image);
              userID = (TextView) v.findViewById(R.id.user_ID);
              userComment = (TextView) v.findViewById(R.id.user_comment_textview);
          }
      }
  
      // Provide a suitable constructor (depends on the kind of dataset)
      public MyAdapter(ArrayList&lt;Comments&gt; myDataset) {
          mDataSet = myDataset;
      }
  
      // Create new views (invoked by the layout manager)
      @Override
      public ViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
          // create a new view
          View v = LayoutInflater.from(parent.getContext())
                  .inflate(R.layout.individual_comment, parent, false);
          // set the view's size, margins, paddings and layout parameters
          return new ViewHolder(v);
      }
  
      // Replace the contents of a view (invoked by the layout manager)
  
      //The OutOfBoundsException is pointing here
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
          Comments comment = mDataSet.get(position);
          holder.userComment.setText(comment.getUserComment());
          holder.userID.setText("User " + position);
      }
  
  
      // Return the size of your dataset (invoked by the layout manager)
      @Override
      public int getItemCount() {
          return mDataSet.size();
      }
  }
  </code></pre>