您可以使用分页概念。
分页:这是一个将文档分成多页的过程。
我用作下面分页:
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左右的数据根据请求的页面。它会让你的应用程序工作得更快。
仰望无尽的列表实现 –
这里是负载更回收查看示例 - https://codentrick.com/load-more-recyclerview-bottom-progressbar/ –
选中此LIB https://github.com/MarkoMilos/Paginate –