1

我正在使用recyclerview进行分页,效果很好,但是当我关闭互联网连接时,它会正常停止,但是当我重新打开并滚动到底部时,除非我重新启动,否则分页不会恢复活动

适配器类(滚动监听器):

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 连接到服务器:

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);
}
4

0 回答 0