uzaysan
uzaysan

Reputation: 605

RecyclerView-How to know if there is 5 items to scroll?

I load data from internet and i'm doing pagination.

I use this code to load more data:

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if(!recyclerView.canScrollVertically(1)&&loading==false&&postson==false){
                loading=true;
                lastitem+=20;
                loaddata(lastitem);

            }
        }


    });

But this only works when recycler view reachjes the bottom and i think this might confuse the user. I want to load more data when 5 item left.

How to do that?

Upvotes: 0

Views: 79

Answers (1)

Xenolion
Xenolion

Reputation: 12735

Check the item count of the layout Manager (make your layout manager global), probably linear layout manager then from there compare the two Like this:

    @Override
    public void onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)
        if (!isRequesting) {
            int visiblePicturePosition = linearLayoutManager.findLastVisibleItemPosition()
            int totalItems = linearLayoutManager.getItemCount()
            if (totalItems - visiblePicturePosition <= 5) {

                //Do what you want here
        }
    }

Upvotes: 1

Related Questions