recyclerview有时会在滚动上获取数据,有时则不会

时间:2017-03-05 10:03:39

标签: android android-studio android-recyclerview

我有一个Recyclerview获取前10个项目,当我向下滚动它有时会得到其他项目,有时不会:

dialog

但有时它起作用并得到其他人(12,11,10,9,8,7,6,5,4,3,2,1 ......)

我的滚动码:

tabindex

获取数据:

item 22
item 21
item 20
item 19
item 18
item 17
item 16
item 15
item 14
item 13
usually it stops at item 13. (not getting the onscroll items)

getdata服务器

  recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                if (isLastItemDisplaying(recyclerView)) {
                    getData();
                }
            }
        });

检查最后一项

 private void getData() {
        requestQueue.add(getDataFromServer(requestCount, user_id));
    }

为什么它有时会起作用的原因,为什么不呢?

---------------------------->修改

private JsonArrayRequest getDataFromServer(String requestCount, String user_id) {
    //Initializing ProgressBar
    final ProgressBar progressBar = (ProgressBar) findViewById(R.id.progressBar1);

    //Displaying Progressbar
    progressBar.setVisibility(View.VISIBLE);

    requestCount = requestCount + "&user=" + user_id;

    //JsonArrayRequest of volley
    JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL + String.valueOf(requestCount),
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    //Calling method parseData to parse the json response
                    parseData(response);
                    //Hiding the progressbar
                    progressBar.setVisibility(View.GONE);
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    progressBar.setVisibility(View.GONE);
                    //If an error occurs that means end of the list has reached
                    Toast.makeText(UserAreaActivity.this, "No More Items Available", Toast.LENGTH_SHORT).show();
                }
            });

    //Returning the request
    return jsonArrayRequest;
}

----------------------------------------- edit 2

 //This method would check that the recyclerview scroll has reached the bottom or not
    private boolean isLastItemDisplaying(RecyclerView recyclerView) {
        if (recyclerView.getAdapter().getItemCount() != 0) {
            int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastCompletelyVisibleItemPosition();
            if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
                return true;
        }
        return false;
    }

1 个答案:

答案 0 :(得分:1)

你试过吗? :

    private boolean isLastItemDisplaying(RecyclerView recyclerView) {
            if (recyclerView.getAdapter().getItemCount() != 0) {
            // instead of findLastCompletelyVisibleItemPosition
                int lastVisibleItemPosition = ((LinearLayoutManager) recyclerView.getLayoutManager()).findLastVisibleItemPosition(); 
                if (lastVisibleItemPosition != RecyclerView.NO_POSITION && lastVisibleItemPosition == recyclerView.getAdapter().getItemCount() - 1)
                    return true;
            }
            return false;
        }