2016-07-16 60 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 
    } 

問我,如果任何困難或不要讓我知道,如果它的工作

相關問題