title: Android Using GridView with OnScrollListener
body: <p>I'm building a gridview that contains alot of images likely around 1000++ pictures. To reduce the loading time for the gridview, I want to implement an onScrollListener using AbsListView. I will limit 20 images per view, then when scrolling reaches bottom it will load another 20 images. This will repeat until all images has been loaded.   </p>
  
  <p>I've been googling samples, but doesnt seem to find one that suits my codes. Could anyone please guide me how it is done? I'm currently using imageloader Lazylist for my images. Also I'm using AsyncTask.  </p>
  
  <p>MainGridView.class</p>
  
  <pre><code>    public class MainGridView extends SherlockActivity {
  
      private ProgressDialog pDialog;
      ArrayList&lt;HashMap&lt;String, String&gt;&gt; songsList;
      static final String KEY_SONG = "song"; 
      static final String KEY_ID = "id";
      static final String KEY_TITLE = "title";
      static final String KEY_ARTIST = "artist";
      static final String KEY_CAT_ARTIST = "artistcat";
      static final String KEY_DURATION = "duration";
      static final String KEY_THUMB_URL = "thumb_url";
      static final String KEY_BIG_URL = "big_url";
      static final String KEY_CAT_URL = "cat_url";
      static String IMAGE_POSITION;
      GridView grid;
      MainGridViewLazyAdapter adapter;
      String cat_url;
      String artist_url;
      private int visibleThreshold = 5;
      private int currentPage = 0;
      private int previousTotal = 0;
      private boolean loading = true;
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.gridview_main);
  
          getSupportActionBar().setDisplayHomeAsUpEnabled(true);
          checkInternetConnection();
  
          grid = (GridView) findViewById(R.id.grid_view);
  
  
  
  
      grid.setOnScrollListener(new OnScrollListener() {
  
      @Override
      public void onScroll(AbsListView view, int firstVisibleItem,
              int visibleItemCount, int totalItemCount) {
          // TODO Auto-generated method stub
          if (loading) {
              if (visibleItemCount &gt; totalItemCount) {
                  loading = false;
                  previousTotal = totalItemCount;
                  currentPage++;
              }
          }
          if (!loading &amp;&amp; (totalItemCount - visibleItemCount) &lt;= (firstVisibleItem + visibleThreshold)) {
              // I load the next page of gigs using a background task,
              // but you can call any function here.
              new loadGridView().execute(currentPage + 1);
              loading = true;
          }
      }
  
      @Override
      public void onScrollStateChanged(AbsListView view, int scrollState) {
          // TODO Auto-generated method stub
  
      }
      });
  
  
  }
  
      public class loadGridView extends AsyncTask&lt;Integer, String, String&gt; {
  
          @Override
          protected void onPreExecute() {
              super.onPreExecute();
              pDialog = new ProgressDialog(MainGridView.this);
              pDialog.setTitle("Connect to Server");
              pDialog.setMessage("This process can take a few seconds to a few minutes, depending on your Internet Connection Speed.");
              pDialog.setIndeterminate(false);
              pDialog.setCancelable(false);
              pDialog.show();
          }
          @Override
          protected String doInBackground(Integer... args) {
              // updating UI from Background Thread
                      Intent in = getIntent();
                      songsList = new ArrayList&lt;HashMap&lt;String, String&gt;&gt;();
                      cat_url = in.getStringExtra(KEY_CAT_URL);
                      artist_url = in.getStringExtra(KEY_CAT_ARTIST);
  
  
                      XMLParser parser = new XMLParser();
                      String xml = parser.getXmlFromUrl(cat_url); // getting XML from URL
                      Document doc = parser.getDomElement(xml); // getting DOM element
  
                      NodeList nl = doc.getElementsByTagName(KEY_SONG);
                      // looping through all song nodes &lt;song&gt;
                      for (int i = 0; i &lt; nl.getLength(); i++) {
                          // creating new HashMap
                          HashMap&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
                          Element e = (Element) nl.item(i);
                          // adding each child node to HashMap key =&gt; value
                          map.put(KEY_ID, parser.getValue(e, KEY_ID));
                          map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));
                          map.put(KEY_ARTIST, parser.getValue(e, KEY_ARTIST));
                          map.put(KEY_DURATION, parser.getValue(e, KEY_DURATION));
                          map.put(KEY_THUMB_URL, parser.getValue(e, KEY_THUMB_URL));
                          map.put(KEY_BIG_URL, parser.getValue(e, KEY_BIG_URL));
                          // adding HashList to ArrayList
                          songsList.add(map);
  
                      }
                      return null;
                  }   
  
  
          @Override
          protected void onPostExecute(String args) {
              ActionBar   ab = getSupportActionBar();
                          ab.setTitle(artist_url);
  
              adapter=new MainGridViewLazyAdapter(MainGridView.this, songsList);   
              grid.setAdapter(adapter);
              pDialog.dismiss();
              }
          }
  </code></pre>