2016-07-16 63 views
1

我正在使用以下URL在一个Android应用程序中从一个URL的10个项目的块中为我的回收站视图获取数据。我无法理解如何更改滚动上的适配器数据集?在Android Recycler中分页查看从URL中获取项目

分页:您可以通过更改API中的开始参数来加载结果的下一页。 例如: 第1页:http://api.smartprix.com/simple/v1?type=search&key=NVgien7bb7P5Gsc8DWqc&category=Mobiles&q=3g&start=0&indent=1 第2页:http://api.smartprix.com/simple/v1?type=search&key=NVgien7bb7P5Gsc8DWqc&category=Mobiles&q=3g&start=10&indent=1

这是我使用从网址得到物品的代码: -

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

检查[这](https://github.com/MarkoMilos/Paginate) – Nisarg

回答

0

采取INT开始= 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 
    } 

问我,如果任何困难或不要让我知道,如果它的工作

相关问题