我必须显示世界上的机场列表,用户必须选择其中一个。所以我做了一个Recyclerview:
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();
其中airports
是List<Airport>
。问题是它在recyclelerview中加载20秒的时间来收取完整列表,当我尝试搜索任何项目时,它是如此之慢。所以我想加载20个机场的时间,然后在用户滚动Recyclerview时加载其他机场。有办法吗?我也想这样做,因为使用searchview搜索它也有点慢,我想快点。我认为notifyDataSetChanged()
预加载它但不起作用......
编辑:这是从Realm中检索我的列表的方法:
final RealmResults<AirportR> airps = realm.where(AirportR.class).findAll();
airports = realm.copyFromRealm(airps, 0);
感谢您的回答...
答案 0 :(得分:4)
您可以在回收视图中使用分页来解决您的问题。
请参阅以下链接。
how to implement pagination in RecyclerView on scroll
https://medium.com/@etiennelawlor/pagination-with-recyclerview-1cb7e66a502b#.gxn5ft3n1
答案 1 :(得分:1)
您可以使用分页概念。
分页:这是将文档分成多个页面的过程。
我使用了以下分页:
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左右的数据。这会让你的应用更快。