无尽的RecyclerView - ProgressBar只有在慢慢滚动时才会消失

时间:2016-03-22 11:40:03

标签: android android-recyclerview

我正在使用RecyclerView并从10个批量中获取API中的对象。对于分页,我使用EndlessRecyclerOnScrollListener并遵循本教程Endless RecyclerView.

我的问题:在滚动程序时必须添加进度条并在从服务器获取数据后删除它。但就我而言,它并不起作用。 ProgressBar还在下载。如果滚动慢,它会消失。如果滚动得更快,进度条不会消失。

Endless RecyclerView

获取数据的方法

private void makeJsonObjectRequest(int startList,int endList,final JsonObjectListener listener) {
            JsonArrayRequest movieReq = new JsonArrayRequest(url,
                    new Response.Listener<JSONArray>() {
                        @Override
                        public void onResponse(JSONArray response) {
                            Log.d(TAG, response.toString());
                            i = startLsit;
                            while(i < endList) {
                                try {
                                    JSONObject obj = response.getJSONObject(i);
                                    Movie movie = new Movie();

                                  /***** TONS OF CODE  *****/

                                        movieList.add(movie);
                                    }

                                    }
                                } catch (JSONException e) {
                                    e.printStackTrace();

                                }
                              i++;
                              lastNumber = i;
                            }
                            mAdapter.notifyDataSetChanged();
                            listener.onDone(lastNumber);
                        }
                    }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());

                }
            });
            AppController.getInstance().addToRequestQueue(movieReq);

        }

我在片段中使用上面的方法:

makeJsonObjectRequest(0, 9, new JsonObjectListener() {
                       @Override
                       public void onDone(int lastNumberI) {
                           lastNumber = lastNumberI;
                       }

                       @Override
                       public void onError(String error) {

                       }
                   }); }

   mAdapter = new RVAdapter(getContext(),movieList,this,mRecyclerView);
        mAdapter.setClickListener(this);
        mRecyclerView.setAdapter(mAdapter);

        mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                movieList.add(null);
                mAdapter.notifyItemInserted(movieList.size()-1);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //   remove progress item
                        movieList.remove(movieList.size()-1);
                        mAdapter.notifyItemRemoved(movieList.size());
                        //add items one by one
                        int start = lastNumber;
                        int end = start + 9;
                        makeJsonObjectRequest(start, end, new JsonObjectListener() {
                            @Override
                            public void onDone(int lastNumberI) {
                                lastNumber = lastNumberI;
                            }

                            @Override
                            public void onError(String error) {

                            }
                        });
                        mAdapter.setLoaded();
                    }
                }, 2000);
            }
        });

我的适配器Endless RecyclerView

我注意到当我滚动更快的代码时,重复

mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore() {
                movieList.add(null);
                mAdapter.notifyItemInserted(movieList.size()-1);
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        //   remove progress item
                        movieList.remove(movieList.size()-1);
                        mAdapter.notifyItemRemoved(movieList.size());
                        //add items one by one
                        int start = lastNumber;
                        int end = start + 9;
                        makeJsonObjectRequest(start, end, new JsonObjectListener() {
                            @Override
                            public void onDone(int lastNumberI) {
                                lastNumber = lastNumberI;
                            }

                            @Override
                            public void onError(String error) {

                            }
                        });
                        mAdapter.setLoaded();
                    }
                }, 2000);
            }
        }); 

1 个答案:

答案 0 :(得分:0)

将它移到runnable之外。否则它将仅在两秒后被删除(或者它根本不会被删除)

mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
        @Override
        public void onLoadMore() {
            movieList.add(null);
            mAdapter.notifyItemInserted(movieList.size()-1);
            //   remove progress item
            movieList.remove(movieList.size()-1);
            mAdapter.notifyItemRemoved(movieList.size());
            handler.postDelayed(new Runnable() {
                @Override
                public void run() {

                    //add items one by one
                    int start = lastNumber;
                    int end = start + 9;
                    makeJsonObjectRequest(start, end, new JsonObjectListener() {
                        @Override
                        public void onDone(int lastNumberI) {
                            lastNumber = lastNumberI;
                        }

                        @Override
                        public void onError(String error) {

                        }
                    });
                    mAdapter.setLoaded();
                }
            }, 2000);
        }
    });