1

i am working on pagination with recyclerview, it works very well, but the moment i switch off my internet connection, it stops normally, but when i put it back on and scroll to the bottom, the pagination doesn't resume unless i restart the activity

Adapter Class(scroll listener):

public ScrollAdapter(ArrayList<ArrayList<String>> datas, Context context, RecyclerView recyclerView) {
    this.datas = datas;
    this.context = context;
    final Context con = context;

    final LinearLayoutManager layoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();

    recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
            if (dy > 0) {
                visibleItemCount = layoutManager.getChildCount();
                totalItemCount = layoutManager.getItemCount();
                pastVisibleItems = layoutManager.findFirstVisibleItemPosition();

                if (loading) {
                    if ((visibleItemCount + pastVisibleItems) >= totalItemCount) {
                        if (onLoadMoreListener != null)
                            onLoadMoreListener.onLoadMore();
                        loading = false;
                    }
                }
            }
        }
    });
}

public void setLoading() {
    loading = true;
}

Volley Connection to Server:

public void doThis() {
    if (CheckClass.getConnectivityStatusBool(this)) {
        //Toast.makeText(this, Integer.toString(pageNo), Toast.LENGTH_SHORT).show();
        makeRequest(pageNo);
        //new LoadRecharge(this, this).execute(Integer.toString(pageNo));
        pageNo += 5;
    } else {
        Toast.makeText(this, "No internet connection", Toast.LENGTH_LONG).show();
        scrollAdapter.setLoading();
    }
}

@Override
public void onLoadMore() {
    if (!total.equals("end")) {
        doThis();
    }
}

public void makeRequest(int pageNo) {
    arr.add(null);
    scrollAdapter.notifyItemInserted(arr.size() - 1);
    final int pagen = pageNo;

    String url = "******";
    RequestQueue queue = Volley.newRequestQueue(this);

    StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
            new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {
                    arr.remove(arr.size() - 1);
                    scrollAdapter.notifyItemRemoved(arr.size());
                    parse(response);
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            if (error instanceof NetworkError) {
                Toast.makeText(Demo.this, "Network error", Toast.LENGTH_SHORT).show();
            } else if (error instanceof ServerError) {
                Toast.makeText(Demo.this, "Server error", Toast.LENGTH_SHORT).show();
            } else if (error instanceof AuthFailureError) {
                Toast.makeText(Demo.this, "Auth error", Toast.LENGTH_SHORT).show();
            } else if (error instanceof ParseError) {
                Toast.makeText(Demo.this, "Parse error", Toast.LENGTH_SHORT).show();
            } else if (error instanceof TimeoutError) {
                Toast.makeText(Demo.this, "Timeout error", Toast.LENGTH_SHORT).show();
            }

            arr.remove(arr.size() - 1);
            scrollAdapter.notifyItemRemoved(arr.size());
        }
    })

    {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> data = new HashMap<>();
            data.put("page", Integer.toString(pagen));
            return data;
        }

    };

    queue.add(stringRequest);
}
Dima Kozhevin
  • 3,091
  • 9
  • 33
  • 48
Tobi Oyelekan
  • 213
  • 4
  • 15
  • 1
    Usually we use loading flag to prevent sending multiple request on RecyclerView scrolls, so I think you should call loadMore when loading is false, also I recommend to add a footer view to let the user load more in case that connection failed, By the way change loading flag only in request and response body. – Farshad Tahmasbi Nov 10 '17 at 12:04
  • Ok.thanks, I'll try that out – Tobi Oyelekan Nov 11 '17 at 09:54

0 Answers0