2016-11-15 79 views
0

我必须显示世界各地的机场列表,用户必须选择其中一个。所以,我已经做了recyclerview:有没有什么方法可以在回收站查看5到10个物品,而不是50k?

RecyclerView recyclerView = (RecyclerView)rootView.findViewById(R.id.recyclerview); 
    SearchView searchView = (SearchView)rootView.findViewById(R.id.searchview); 

    recyclerView.setHasFixedSize(true); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    final AirportAdapter adapter = new AirportAdapter(airports,getActivity()); 
    recyclerView.setAdapter(adapter); 
    adapter.notifyDataSetChanged(); 

其中airportsList<Airport>。问题是,它加载20秒钟的时间来为recyclerview中的完整列表充电,当我尝试搜索任何项目时,速度如此之慢。因此,我想要为20个机场加载时间,然后在用户滚动回收站视图时加载其他机场。有没有办法做到这一点?我也想这样做,因为使用searchview进行搜索它也有点慢,并且我想让它快速。我认为notifyDataSetChanged()预紧力,但它不工作...

编辑:这是如何从境界找回我的列表:

final RealmResults<AirportR> airps = realm.where(AirportR.class).findAll(); 
airports = realm.copyFromRealm(airps, 0); 

谢谢你的答案...

+0

仰望无尽的列表实现 –

+0

这里是负载更回收查看示例 - https://codentrick.com/load-more-recyclerview-bottom-progressbar/ –

+0

选中此LIB https://github.com/MarkoMilos/Paginate –

回答

0

您可以使用分页概念。

分页:这是一个将文档分成多页的过程。

我用作下面分页:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() { 
     @Override 
     public void onScrolled(RecyclerView recyclerView, int dx, int dy) { 
      LinearLayoutManager linearLayoutManager= (LinearLayoutManager) recyclerView.getLayoutManager(); 


    /*countToShowLoadButton here is total number of items available to 
    *user after no. of page requests. So when total visible item would 
    *reach to the end (i.e. countToShowLoadButton-3) then a "load more" 
    *button would appear and it will call a new request loadMoreData 
    */ 

      if(linearLayoutManager.findLastVisibleItemPosition() > countToShowLoadButton-3 && currentPageNumber < totalPageNumber){ 
       loadMoreButton.setVisibility(View.VISIBLE); 
       loadMoreButton.setEnabled(true); 
      } 
      super.onScrolled(recyclerView, dx, dy); 
     } 
    }); 

loadMoreData:

public void loadMoreData(View view){ 
    getAllData(currentPageNumber+1); 
} 

getAllData:

public void getAllData(int pageNo){ 

    progressBar.setVisibility(View.VISIBLE); 

    final String key = sharedPreferences.getString("Auth_key",null); 

    String pageNumber = String.valueOf(pageNo); 

    checkInternet = new InternetConnectivityChecker(getApplicationContext()); 

    if(checkInternet.isOnline()) { 

     StringRequest stringRequest = new StringRequest(Request.Method.GET, 
       URL_string.api_url +"?"+pageNumber, 
       new Response.Listener<String>() { 
        @Override 
        public void onResponse(String response) { 
         Log.i("Result", "Success \n" + response); 

         try { 
          JSONObject responseJSON = new JSONObject(response); 

          JSONArray dataPart = responseJSON.getJSONArray("data"); 
          JSONObject metaPart = responseJSON.getJSONObject("meta").getJSONObject("pagination"); 

//Here it updates all the variable so that when user clicks on "load more" 
// button again then it loads the data from new page 

          currentPageNumber = metaPart.getInt("current_page"); 
          totalPageNumber = metaPart.getInt("total_pages"); 
          totalNumberOfData = metaPart.getInt("total"); 
          perPageItems = metaPart.getInt("per_page"); 
          dataAtShownPage = metaPart.getInt("count"); 
          countToShowLoadButton = perPageItems * currentPageNumber; 


//Here it adds new data to the shown page of the app 
          prepareValueWithServerData(dataPart,dataAtShownPage); 


         } catch (Exception e) { 
          Toast.makeText(getApplicationContext(),noNetConnMessage,Toast.LENGTH_LONG).show(); 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 
       Toast.makeText(getApplicationContext(),noNetConnMessage,Toast.LENGTH_LONG).show(); 
      } 
     }) { 

      @Override 
      public Map<String, String> getHeaders() throws AuthFailureError { 
       Map<String, String> header = new HashMap<>(); 
       header.put("Authorization", "Bearer " + key); 
       return header; 
      } 
     }; 

     RequestQueue requestQueue = Volley.newRequestQueue(this); 
     stringRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
     requestQueue.add(stringRequest); 

     if(stringRequest.getTimeoutMs() > DefaultRetryPolicy.DEFAULT_TIMEOUT_MS*2){ 
      requestQueue.stop(); 
      Toast.makeText(getApplicationContext(),noNetConnMessage,Toast.LENGTH_LONG).show(); 
      progressBar.setVisibility(View.INVISIBLE); 
     } 

    } else { 
     Toast.makeText(getApplicationContext(),"Please Check Internet Connection",Toast.LENGTH_LONG).show(); 
    } 

} 

PrepareValueWithServerData:

public void prepareValueWithServerData(JSONArray data, int count){ 

    progressBar.setVisibility(View.INVISIBLE); 

    for(int i=0; i<count; i++){ 
     try { 
      JSONObject singleItem = data.getJSONObject(i); 
      item_details a = new item_details(details_1,details_2,..); 
      list.add(a); 


//notifying adapter that data has been changed in the list 
      adapter.notifyDataSetChanged(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 
    } 

} 

注:服务器将不得不返回数据按照页面计数即分页概念将在服务器端编程中使用,以便它不会立刻返回50K的数据,否则它应该只是返回10-20左右的数据根据​​请求的页面。它会让你的应用程序工作得更快。

相关问题