2016-07-16 17 views
1

Ich arbeite an einer Android-App, wo ich Daten für meine Recycler-Ansicht von einer URL in Chuncks von 10 Elementen auf einmal unter Verwendung der folgenden URLs abrufen. Ich bin nicht in der Lage zu verstehen, wie man den Adapterdatensatz beim Scrollen ändert.Paginierung in Android Recycler Anzeigen von Elementen aus URLs

Seitenumbruch: Sie können die nächste Ergebnisseite laden, indem Sie den Startparameter in der API ändern. Beispiel: Seite 1: http://api.smartprix.com/simple/v1?type=search&key=NVgien7bb7P5Gsc8DWqc&category=Mobiles&q=3g&start=0&indent=1 Seite 2: http://api.smartprix.com/simple/v1?type=search&key=NVgien7bb7P5Gsc8DWqc&category=Mobiles&q=3g&start=10&indent=1

Dies ist der Code, den ich Elemente aus der URLs erhalten bin mit: -

private class FetchProductsTask extends AsyncTask<String, String, String> { 

    @Override 
    protected String doInBackground(String... params) { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpResponse response; 
     String responseString = null; 
     try { 
      // make a HTTP request 
      response = httpclient.execute(new HttpGet(params[0])); 
      StatusLine statusLine = response.getStatusLine(); 
      if (statusLine.getStatusCode() == HttpStatus.SC_OK) { 
       Log.d(TAG,"HTTP request status is OK"); 
       ByteArrayOutputStream out = new ByteArrayOutputStream(); 
       response.getEntity().writeTo(out); 
       out.close(); 
       responseString = out.toString(); 
      } else { 
       // close connection 
       Log.d(TAG,"HTTP request status is close connection"); 
       response.getEntity().getContent().close(); 
       throw new IOException(statusLine.getReasonPhrase()); 
      } 
     } catch (Exception e) { 
      Log.d(TAG, "Couldn't make a successful request!"+e); 
     } 
     return responseString; 
    } 

    @Override 
    protected void onPostExecute(String response) { 
     super.onPostExecute(response); 

     try { 
      // convert the String response to a JSON object 
      Log.d(TAG,"Response = "+response); 
      JSONObject jsonResponse = new JSONObject(response); 

      // fetch the array of movies in the response 
      JSONArray jArray = jsonResponse.getJSONObject("request_result").getJSONArray("results"); 
      Log.d(TAG,"jArray = "+jArray.toString()); 


      for (int i = 0; i < jArray.length(); i++) { 

       Log.d(TAG,"product = "+jArray.get(i)); 
       JSONObject product = jArray.getJSONObject(i); 
       products.add(new ProductDataForList(product.getString("id"),product.getString("name"),product.getString("img_url"),product.getInt("price"))); 
       /*movieTitles.add(movie.getString("title")); 


       movieSynopsis.add(movie.getString(movie.getString("synopsis"))); 
       movieImgUrl.add(movie.getString(movie.getString("id")));*/ 
      } 
      // refresh the ListView 
      fillProductList(products); 
     } catch (JSONException e) { 
      Log.d("Test", "Couldn't successfully parse the JSON response!"); 
     } 
    } 
} 
+0

prüfen arbeitete [diese] (https://github.com/MarkoMilos/Paginate) – Nisarg

Antwort

0

einen int Start nehmen = 0;

recyclerview.addOnScrollListener(new RecyclerView.OnScrollListener() { 
     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
      super.onScrolled(recyclerView, dx, dy); 
      int lastItem = mLayoutManager.findLastCompletelyVisibleItemPosition(); 
      if (lastItem == ArrayList.size() - 1) { 
      start=start+10; 
      //now hit you api 
      hitapi(start); 

     } 
    }); 
    hitapi(int start){ 
     //in your api set this start parameter value 
     //weher you recive the response add your new list to the bottom of new list and call adapter.notifydatasetchanged 
     responseArrayList.clear(); 
     responseArrayList.addAll(flaersPOJO.flares); 
     ArrayList<Flare> newList = new ArrayList<Flare>(OrignalArrayList); 
     newList.addAll(responseArrayList); 
     OrignalArrayList.clear(); 
     OrignalArrayList.addAll(newList); 
     adapter.notifyDataSetChanged(); 
     //try somtinhg like this 
    } 

fragen Sie mich, wenn irgendwelche Schwierigkeiten oder lassen Sie mich wissen, wenn es