title: removing RecyclerView items
body: <p>I have a RecyclerView which is adding items each certain period. When adding objects, if list items are more than (lets say) 500, then the first items will be removed and the new items will be added.</p>
  
  <p>If the RecyclerView cannot scroll down any more <code>!recyclerView.canScrollVertically(1);</code> then, after adding new items the RecyclerView will <code>smoothScroolToPosition()</code> to the last position. </p>
  
  <p><strong>Where is the problem?</strong>
  Okay, if ReyclerView is at the middle (not bottom, not top) when removing old items, it will suddenly jump some positions up. I want the RecyclerView to not jump position and stay where it was when removing items at the top</p>
  
  <p>I tried using <code>layoutManager.setStackFromEnd(true);</code> but no luck</p>
  
  <p>Any suggestions?</p>
  
  <p>Some code (unrelated code is deleted). My problem can be reproduced when the RecyclerView is showing list items from the middle and press VOLUME_UP:</p>
  
  <pre><code>public class ActivityMain extends ActionBarActivity {
  
      public static final int MAX_LOG_ITEMS = 500;
  
      private RecyclerView mRecyclerView;
      private AdapterLog mRecyclerAdapter;
      private boolean mAutoScroll = true;
  
      private DataReceiver mDataReceiver;
      private Handler mLogHandler = new Handler() {
          @Override public void handleMessage(Message msg) {
              switch (msg.what){
                  case DataReceiver.CAT_LOGS:
                      List&lt;Log&gt; catLogs = (List&lt;Log&gt;) msg.obj;
                      updateLogs(catLogs);
                      break;
                  case DataReceiver .CLEAR_LOGS:
                      if (mRecyclerAdapter.getItemCount() &gt; MAX_LOG_ITEMS)
                          mRecyclerAdapter.removeFirstItems(mRecyclerAdapter.getItemCount() - MAX_LOG_ITEMS);
                      break;
                  case Logcat.REMOVE_LOGS:
                      mRecyclerAdapter.clear();
              }
          }
  
      };
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
  
          final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
          layoutManager.setStackFromEnd(true);
          mRecyclerAdapter = new AdapterLog();
  
          mRecyclerView = (RecyclerView) findViewById(R.id.activity_main_recyclerview);
          mRecyclerView.setHasFixedSize(true);        
          mRecyclerView.setLayoutManager(layoutManager);
          mRecyclerView.setAdapter(mRecyclerAdapter);
          mRecyclerView.setOnScrollListener(new UIUtils.ScrollManager(toolbarContainer != null ?
                  toolbarContainer : toolbar){
                  @Override public void onScrolled(RecyclerView r, int dx, int dy) {
                      super.onScrolled(r, dx, dy);
                      mAutoScroll = !r.canScrollVertically(1);
                      }
                  });
      }
  
  
  
      @Override
      public boolean onKeyDown(int keyCode, KeyEvent event) {
          switch(keyCode){
              case KeyEvent.KEYCODE_VOLUME_UP:
  //                mAutoScroll = false;
  //                mRecyclerView.scrollToPosition(0);
  //                if (mRecyclerAdapter.getItemCount() &gt; MAX_LOG_ITEMS)
                      mRecyclerAdapter.removeFirstItems(mRecyclerAdapter.getItemCount() - 50);
  
                  return true;
              case KeyEvent.KEYCODE_VOLUME_DOWN:
                  mAutoScroll = true;
                  mRecyclerView.scrollToPosition(mRecyclerAdapter.getItemCount() -1);
                  return true;
          }
          return false;
      }
  
      private void updateLogs(final List&lt;Log&gt; logList) {
          final boolean scroll = mAutoScroll;
          mRecyclerAdapter.addAll(logList);
          if (scroll) mRecyclerView.smoothScrollToPosition(mRecyclerAdapter.getItemCount() - 1);
      }
  
  }
  </code></pre>
  
  <p>The RecyclerAdapter:</p>
  
  <pre><code>public class AdapterLog extends RecyclerView.Adapter&lt;AdapterLog.ViewHolder&gt; {
  
      private final List&lt;Log&gt; mLogList;
  
      public AdapterLog() {
          this.mLogList = new ArrayList&lt;Log&gt;();
      }
  
      @Override
      public AdapterLog.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
          View v = LayoutInflater.from(parent.getContext())
              .inflate(R.layout.listitem_log, parent, false);
          return new ViewHolder(v);
      }
  
      @Override
      public void onBindViewHolder(ViewHolder holder, int position) {
          holder.mTextView.setText(getItem(position).getMessage());
          holder.mTextView.setTextColor(getItem(position).getLevel().getColor());
      }
  
      @Override
      public int getItemCount() {
          return mLogList.size();
      }
  
      public Log getItem(int position) {
          return mLogList.get(position);
      }
  
      public void addAll(List&lt;Log&gt; logList) {
          mLogList.addAll(logList);
          notifyDataSetChanged();
      }
  
      public void removeFirstItems(int count) {
          for (int i=0; i&lt;count; i++) mLogList.remove(0);
          notifyDataSetChanged();
      }
  
      public void clear() {
          mLogList.clear();
          notifyDataSetChanged();
      }
  
      public static class ViewHolder extends RecyclerView.ViewHolder {
          public TextView mTextView;
          public ViewHolder(View v) {
              super(v);
              mTextView = (TextView) v.findViewById(R.id.listitem_log_textview);
          }
      }
  }
  </code></pre>