title: How to implement the Endless scroll listener for a gridview
body: <p>i have been trying to implement the endless scroll feature for my product listing page of a ecommmerce app. The grids show the details of the items and when i scroll to the bottom i need to show a progress bar and then append the new grid of items. </p>
  
  <p>The api call works like this, I need to send a start_row_number and limit, which will send me all the items from the start_row_number to limit. Example: start_row_number = 0 and limit = 10. This will return items from 0 to 10</p>
  
  <p>After that i need to load more items when the user reaches the bottom of the grid, and append it to the gridview. So i will send start_row_number = 10 and limit = 10, this will return items form 10 to 20. </p>
  
  <p>As of now, i can get the items from 0 to 10 but not after that. How can i create the endless scroll feature and make everything such that it doesn't give me error such as 'too much work on the main thread'</p>
  
  <p>Here is my MainActivity:</p>
  
  <pre><code>public class ProductListing extends Activity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.product_listing_act);
      init();
  }
  
  public void productListingApiCall(ProductListingCondtionModel productListingCondtionModel) {
  
      RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(productListingCondtionModel.getBase_url()).setLogLevel(RestAdapter.LogLevel.FULL).build();
      final ProductListingApi productListingApi =
              restAdapter.create(ProductListingApi.class);
      productListingApi.getFeed(productListingCondtionModel.getFile(),
              productListingCondtionModel.getOperation_condition(),
              productListingCondtionModel.getSearch_string_condition(),
              productListingCondtionModel.getMinprice_condition(),
              productListingCondtionModel.getMaxprice_condition(),
              productListingCondtionModel.getMincusratings_condition(),
              productListingCondtionModel.getMaxcusratings_condition(),
              productListingCondtionModel.getDiscount_condition(),
              productListingCondtionModel.getCatids_condition(),
              productListingCondtionModel.getBrands_condition(),
              productListingCondtionModel.getAffids_condition(),
              productListingCondtionModel.getStart_row_condition(),
              productListingCondtionModel.getLimit(),
              productListingCondtionModel.getOrderby_condition(),
              productListingCondtionModel.getSortby_condition(), new Callback&lt;ProductListingPojo&gt;() {
  
                  @Override
                  public void success(ProductListingPojo productListingPojo, Response response) {
                      final ProductListingPojo product = productListingPojo;
                      new Thread(new Runnable() {
                          @Override
                          public void run() {
                              String[] t = Arrays.copyOf(product.getTitle(),
                                      product.getTitle().length);
                              int[] p = Arrays.copyOf(product.getSellingprice(),
                                      product.getSellingprice().length);
                              int[] m = Arrays.copyOf(product.getMrp(),
                                      product.getMrp().length);
                              int[] d = Arrays.copyOf(product.getDiscountpercent(),
                                      product.getDiscountpercent().length);
                              String[] i = Arrays.copyOf(product.getProductimageSmall1(),
                                      product.getProductimageSmall1().length);
  
  
                              for(int j = 0; j &lt; t.length; j++) {
                                  CategoryAllApi categoryAllApi = new CategoryAllApi();
                                  categoryAllApi.setTitle(t[j]);
                                  categoryAllApi.setPrice(p[j]);
                                  categoryAllApi.setMrp(m[j]);
                                  categoryAllApi.setDiscount(d[j]);
                                  categoryAllApi.setImage(i[j]);
                                  arrayList.add(categoryAllApi);
                              }
  
  
  
                          }
                      }).run();
                      setAdapter();
                  }
  
                  @Override
                  public void failure(RetrofitError error) {
                      tv_title_header.setText(error.getMessage());
                      Log.e("error", error.getMessage());
                  }
              });
  }
  
  void setAdapter() {
      adapter = new ProductListingGridAdapter(this, arrayList);
      gv_product_listing_act.setAdapter(adapter);
  }
  }
  </code></pre>
  
  <p>Heres the Adapter:</p>
  
  <pre><code>public class ProductListingGridAdapter extends BaseAdapter {
  
  public ProductListingGridAdapter(ProductListing productListing, ArrayList&lt;CategoryAllApi&gt; arrayList) {
      this.arrayList= arrayList;
      context = productListing;
      inflater = ( LayoutInflater )context.
              getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
  }
  
  @Override
  public int getCount() {
      return arrayList.size();
  }
  
  @Override
  public Object getItem(int position) {
      return position;
  }
  
  @Override
  public long getItemId(int position) {
      return position;
  }
  
  public class Holder
  {
      ImageView im_pic;
      TextView tv_title, tv_price, tv_mrp, tv_discount;
  }
  
  
  
  @Override
  public View getView(final int position, View convertView, ViewGroup parent) {
      GridView grid = (GridView) parent;
      DisplayMetrics metrics = context.getResources().getDisplayMetrics();
      int width = metrics.widthPixels;
      grid.setColumnWidth(width);
      grid.setNumColumns(2);
      int size = grid.getRequestedColumnWidth() / 2 ;
      Double d = new Double(size * 2);
      int h = d.intValue();
  
      Holder holder = new Holder();
      View rowView;
  
  
      int index = grid.getFirstVisiblePosition();
      View v = grid.getChildAt(0);
      int top = (v == null) ? 0 : (v.getTop() - grid.getPaddingTop());
  
      grid.setSelectionFromTop(index, top);
  
      rowView = inflater.inflate(R.layout.product_listing_gv_items_lay, null);
      rowView.setLayoutParams(new GridView.LayoutParams(size, h));
  
      holder.im_pic = (ImageView) rowView.findViewById(R.id.im_product_listing_gv_items_lay_pic);
      holder.tv_title = (TextView) rowView.findViewById(R.id.tv_product_listing_gv_items_lay_title);
      holder.tv_price = (TextView) rowView.findViewById(R.id.tv_product_listing_gv_items_lay_price);
      holder.tv_mrp = (TextView) rowView.findViewById(R.id.tv_product_listing_gv_items_lay_mrp);
      holder.tv_discount = (TextView) rowView.findViewById(R.id.tv_product_listing_gv_items_lay_discount);
  
      holder.tv_title.setTypeface(EasyFonts.robotoMedium(rowView.getContext()));
      holder.tv_price.setTypeface(EasyFonts.robotoBlack(rowView.getContext()));
      holder.tv_mrp.setTypeface(EasyFonts.robotoLight(rowView.getContext()));
      holder.tv_mrp.setPaintFlags(holder.tv_mrp.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);
      holder.tv_discount.setTypeface(EasyFonts.robotoLight(rowView.getContext()));
  
      categoryAllApi = arrayList.get(position);
      Ion.with(holder.im_pic).load(categoryAllApi.getImage());
      holder.tv_title.setText(categoryAllApi.getTitle());
      holder.tv_price.setText("Rs. " + categoryAllApi.getPrice());
      holder.tv_mrp.setText("Rs. " + categoryAllApi.getMrp());
      holder.tv_discount.setText("" + categoryAllApi.getDiscount() + "%");
  
      rowView.setOnClickListener(new View.OnClickListener() {
  
          @Override
          public void onClick(View v) {
              Intent intent = new Intent(context, ProductDetails.class);
              Bundle bundle = new Bundle();
              bundle.putString("operation", "");
              bundle.putString("productkey", "");
              intent.putExtras(bundle);
              context.startActivity(intent);
  
          }
      });
  
      return rowView;
  }
  
  }
  </code></pre>
  
  <p>Heres the CategoryApiCall.java:</p>
  
  <pre><code>public class CategoryAllApi {
  
  private String title, image;
  private int price, mrp, discount;
  
  public String getTitle() {
      return title;
  }
  
  public void setTitle(String title) {
      this.title = title;
  }
  
  public String getImage() {
      return image;
  }
  
  public void setImage(String image) {
      this.image = image;
  }
  
  public int getPrice() {
      return price;
  }
  
  public void setPrice(int price) {
      this.price = price;
  }
  
  public int getMrp() {
      return mrp;
  }
  
  public void setMrp(int mrp) {
      this.mrp = mrp;
  }
  
  public int getDiscount() {
      return discount;
  }
  
  public void setDiscount(int discount) {
      this.discount = discount;
  }
  }
  </code></pre>