title: android recyclerview json load more items
body: <p>I'm having this problem a little while, I need to load 25+ items from a database in json. When I load all of the including images etc. the app takes ages to load all of them. So I thought could I load the first five and when I scroll to the bottom the second five and so on. But it does not work. Here is my code:</p>
  
  <p>Scroll listener:</p>
  
  <pre><code>rec.addOnScrollListener(new RecyclerView.OnScrollListener() {
          @Override
          public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
              visible = lin.getChildCount();
              total = lin.getItemCount();
              past = lin.findFirstVisibleItemPosition();
  
              if ((visible + past) &gt;= total){
                  new HttpAsyncTask().loadMore();
              }
          }
      });
  </code></pre>
  
  <p>AsyncTask:</p>
  
  <pre><code>private class HttpAsyncTask extends AsyncTask&lt;String, Void, List&lt;PostsData&gt;&gt; {
      int next;
      @Override
      protected List&lt;PostsData&gt; doInBackground(String... params) {
          try {
              //get json from url
              JSONObject object = new JSONObject("{'posts':"+GET("http://www.website.com/json")+"}");
              //get json array
              JSONArray array = object.getJSONArray("posts");
                  for (int i = 0; i &lt; 5; i++) {
                      next = i;
                      PostsData data = new PostsData();
                      //get object from array
                      JSONObject jsonObject = array.getJSONObject(i);
                      //title
                      data.setTitle(jsonObject.getString("name"));
                      //id
                      data.setId(jsonObject.getInt("id"));
                      JSONObject cat = jsonObject.getJSONObject("category");
                      //category name
                      data.setCatagorie(cat.getString("name"));
                      JSONObject img = jsonObject.getJSONObject("thumbnails");
                      //image url
                      String imgUrl = img.getString("preview_url");
                      //convert url into bitmap
                      data.setImage(getBitmap(imgUrl));
  
                      //get the post submitter name from array 'makers'
                      String makerFullname = "";
                      JSONArray userArray = jsonObject.getJSONArray("makers");
                      for (int j = 0; j &lt; userArray.length(); j++) {
                          JSONObject user = userArray.getJSONObject(j);
                          //submitter full name
                          makerFullname = user.getString("full_name");
                          data.setUser(makerFullname);
                      }
  
                      data.setSubmitUser(jsonObject.getJSONObject("submitter").getString("full_name"));
  
                      //get the like count of the post
                      data.setLikeCount(jsonObject.getString("upvotes_count"));
  
                      list.add(data);
                  }
          }catch (JSONException e){
              e.printStackTrace();
          }
          return list;
      }
  
      // onPostExecute displays th
      // results of the AsyncTask.
      @Override
      protected void onPostExecute(List&lt;PostsData&gt; result) {
          adapter = new PostsAdapter(result, getContext());
          rec.setAdapter(adapter);
      }
  
      public void loadMore(){
          try {
              //get json from url
              JSONObject object = new JSONObject("{'posts':" + GET("http://www.materialup.com/api/v1/posts") + "}");
              //get json array
              JSONArray array = object.getJSONArray("posts");
              if (array.length() &lt;= next) {
                  for (int i = 0; i &lt; next + 5; i++) {
                      next = i;
                      PostsData data = new PostsData();
                      //get object from array
                      JSONObject jsonObject = array.getJSONObject(i);
                      //title
                      data.setTitle(jsonObject.getString("name"));
                      //id
                      data.setId(jsonObject.getInt("id"));
                      JSONObject cat = jsonObject.getJSONObject("category");
                      //category name
                      data.setCatagorie(cat.getString("name"));
                      JSONObject img = jsonObject.getJSONObject("thumbnails");
                      //image url
                      String imgUrl = img.getString("preview_url");
                      //convert url into bitmap
                      data.setImage(getBitmap(imgUrl));
  
                      //get the post submitter name from array 'makers'
                      String makerFullname = "";
                      JSONArray userArray = jsonObject.getJSONArray("makers");
                      for (int j = 0; j &lt; userArray.length(); j++) {
                          JSONObject user = userArray.getJSONObject(j);
                          //submitter full name
                          makerFullname = user.getString("full_name");
                          data.setUser(makerFullname);
                      }
  
                      data.setSubmitUser(jsonObject.getJSONObject("submitter").getString("full_name"));
  
                      //get the like count of the post
                      data.setLikeCount(jsonObject.getString("upvotes_count"));
  
                      list.add(data);
                      adapter.notifyDataSetChanged();
                  }
              }
              }catch(JSONException e){
                  e.printStackTrace();
              }
      }.....
  </code></pre>
  
  <p>It just won't load the next 5 items in the recyclerview. I searched on google but I did not really understand how load more works.</p>
  
  <p>Thanks in advance, Sven.</p>
  
  <p>This is the tutorial I followed: <a href="http://www.worldbestlearningcenter.com/tips/Android-ListView-load-more-items.htm" rel="nofollow">here</a></p>