N
N
Nubbin2017-07-24 14:34:03
Android
Nubbin, 2017-07-24 14:34:03

RecyclerView pagination?

Hello guys what is the problem here.

//MainActivity.java
    private int previousTotal = 0;
    private boolean loading = true;
    private int visibleThreshold = 5;
    int firstVisibleItem, visibleItemCount, totalItemCount;
    private int currentPage = 0;



    @Override
    public onCreate(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
listView = (RecyclerView) findViewById(R.id.recycler);
        listView.addOnScrollListener(new RecyclerView.OnScrollListener()
        {
        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) 
    {
                super.onScrolled(recyclerView, dx, dy);

                visibleItemCount = recyclerView.getChildCount();
                totalItemCount = layoutManager.getItemCount();
                firstVisibleItem = layoutManager.findFirstVisibleItemPosition();

                if (loading)
                {
                    if (totalItemCount > previousTotal) {
                        loading = false;
                        previousTotal = totalItemCount;
                        currentPage++;
                    }
                }

                if (!loading && (totalItemCount - visibleItemCount)
                        <= (firstVisibleItem + visibleThreshold)) {
                    // End has been reached

                        load();

                        loading = true;
                }

            }
        });
                        this.load();
}

    protected void loadI()
    {
        Call<list> call = api().list(10 * currentPage);
        call.enqueue(new Callback<UserItems>()
        {
            @Override
            public void onResponse(Call<list> call, Response<list> response)
            {
                if(response.isSuccessful())
                {

                    for (Items item : response.body().items())
                    {
                        Log.d("items", String.valueOf(item.id()));
                    }
                }
                adapter.notifyDataSetChanged();
            }
            @Override
            public void onFailure(@NonNull Call<UserItems> call, @NonNull Throwable t) {
                Log.d("error_messages", "Данные не получены");
            }
        });

    }

This code should be displayed page by page, but it pulls out several pages at once, which could be a problem.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question