I am performing pagination in recyclerView. My data is loading successfully and problem is how can I request to server for the next page. How i can perform pagination and how i can find page number which is being scrolled.
My code is:
rvLatestProduct.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//super.onScrolled(recyclerView, dx, dy);
visibleItemCount = rvLatestProduct.getChildCount();
totalItemCount = linearLayoutManager.getItemCount();
firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
Log.e("totalItemCount",String.valueOf(totalItemCount));
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
// currentPage += 1;
previousTotal = totalItemCount;
Log.e("previousTotal",String.valueOf(previousTotal));
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
//int initialSize = totalItemCount.size;
getPagination();
//val updatedSize = dataList.size
// recyclerView.post { adapter.notifyItemRangeInserted(initialSize, updatedSize) }
loading = true;
}
}
});
and getPagination(); code is given as
private void getPagination(){
final JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET,
AllURLs.viewAllLatestProduct,null,
new Response.Listener<JSONArray>() {
JSONArray object;
@Override
public void onResponse(JSONArray response) {
try {
for (int i=0;i<response.length();i++) {
object = response.getJSONArray(i);
}
setLatestProductAdapter(object);
adapter.notifyDataSetChanged();
// Toast.makeText(context,"page no" + pageNo,Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
VolleySingleton.getInstance(context).addToRequestQueue(request);
}