title: GridView with infinite scroll optimization
body: <p>I've created a simple app that fetches images from Pixabay and then displays them in a <code>GridView</code> with infinite scroll. </p>
  
  <p>My <code>OnScrollListener</code>:</p>
  
  <pre><code>public class BasicOnScrollListener implements AbsListView.OnScrollListener {
  
  private IOnScroll onScroll;
  
  public BasicOnScrollListener(IOnScroll action) {
      this.onScroll = action;
  }
  
  @Override
  public void onScrollStateChanged(AbsListView view, int scrollState) {
  }
  
  @Override
  public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
      if (firstVisibleItem + visibleItemCount &gt;= totalItemCount - visibleItemCount)
          onScroll.onReachedEnd();
  }
  }
  </code></pre>
  
  <p>Code resposible for data handling:</p>
  
  <pre><code>private List&lt;Image&gt; images = new ArrayList&lt;&gt;();
  
  ....
  
  private void init() {
      this.imageAdapter = new ImageAdapter(this, images);
      this.gridView.setAdapter(imageAdapter);
  
      populateGridView();
      this.gridView.setOnScrollListener(new BasicOnScrollListener(() -&gt; {
          populateGridView();
      }));
  }
  
  private void  populateGridView() {
      if (!isLoading) {
          isLoading = true;
          this.progressBar.setVisibility(View.VISIBLE);
          imagesRepository.getImages(currentPage, PAGE_SIZE, new IOnRepositoryDataReturn&lt;ImagesList&gt;() {
              @Override
              public void onData(ImagesList data) {
                  clearIfNeeded();
                  images.addAll(data.images);
                  imageAdapter.notifyDataSetChanged();
                  onFinish();
              }
  
              @Override
              public void onError(String error) {
                  Toast.makeText(getApplicationContext(), error, Toast.LENGTH_LONG).show();
              }
  
              private void clearIfNeeded() {
                  if (images.size() &gt; 1000) {
                      images.subList(0, 300).clear();
                  }
              }
  
              private void onFinish() {
                  progressBar.setVisibility(View.INVISIBLE);
                  isLoading = false;
                  currentPage = currentPage + 1;
              }
          });
      }
  }
  </code></pre>
  
  <p>Everything works fine but I'd like to optimize that. When there are already more than 1000 items in the <code>GridView</code> I'd like to remove the first 300 items, so I won't run into out of memory problems. </p>
  
  <p>The problem is, when I simply remove the first 300 items from list (as shown in <code>clearIfNeeded()</code> method in <code>IOnRepositoryDataReturn</code> implementation), the screen shifts. I no longer see items that I've seen before removal.</p>
  
  <p>Example image. Situation if first row (items 1-2) from <code>images</code> list is removed. </p>
  
  <ul>
  <li>left image - grid before removing</li>
  <li>center - grid after removing (as it is for now, removing items on top shifts all items up)</li>
  <li>right - how i'd like it to behave (removing items somewhere up doesnt affect what is seen)</li>
  </ul>
  
  <p>The black square is representing the <code>GridView</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/cvjNU.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/cvjNU.jpg" alt="enter image description here"></a></p>
  
  <p>I'd like to somehow adjust the viewing position in <code>GridView</code>, so I'd still see the same images as before removal.</p>
  
  <p>Layout xml: </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MainActivity"&gt;
  
      &lt;ProgressBar
          android:id="@+id/ProgressSpinner"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:layout_marginBottom="8dp"
          android:layout_marginEnd="8dp"
          android:layout_marginStart="8dp"
          android:layout_marginTop="8dp"
          app:layout_constraintBottom_toBottomOf="@+id/GridView"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="@+id/GridView" /&gt;
  
      &lt;GridView
          android:id="@+id/GridView"
          android:layout_width="match_parent"
          android:layout_height="406dp"
          android:layout_marginBottom="8dp"
          android:numColumns="3"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent"&gt;
  
      &lt;/GridView&gt;
  </code></pre>
  
  <p></p>
  
  <p>Is it even possible with <code>Grid View</code> or should i look for some other possibility?</p>